Hi all,
It seems that I misundertood how the transparent persistent collection (managed by reachability) worked with Hibernate.
Here is a sample configuration hbm file with a bidirectionnal Parent-Child relationship:
Code:
<class name="com.oalia.apps.purchaser.bo.common.nomenclature.NomenclatureItem" table="NOMENCLATURE_ITEM"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="1">
<id name="id" column="ID" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="NAME"
not-null="false"
unique="false"
/>
<property
name="rank"
type="java.lang.Integer"
update="true"
insert="true"
column="RANK"
not-null="false"
unique="false"
/>
<many-to-one
name="nomenclature"
class="com.oalia.apps.purchaser.bo.common.nomenclature.Nomenclature"
cascade="none"
outer-join="auto"
update="false"
insert="false"
column="NOMENCLATURE_ID"
not-null="true"
/>
</class>
<class name="com.oalia.apps.purchaser.bo.common.nomenclature.Nomenclature"
table="NOMENCLATURE"
dynamic-update="false"
dynamic-insert="false">
<id name="id" column="ID" type="java.lang.Integer" unsaved-value="null">
<generator class="native"/>
</id>
<list name="items" lazy="false" inverse="true" cascade="all">
<key column="NOMENCLATURE_ID"/>
<index column="RANK"/>
<one-to-many class="com.oalia.apps.purchaser.bo.common.nomenclature.NomenclatureItem"/>
</list>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="NAME"
not-null="false"
unique="false"/>
</class>
The main problem is that index column and the represented child field (rank) are not updated. That means that:
Code:
nomenclature.getItems().add(new NomenclatureItem("n1"));
nomenclature.getItems().add(new NomenclatureItem("n2"));
the ranks are not set, and when retrieving the items in another session, the objects will not have the correct index.
The other problem is that the collection is not as persistent as it seams.
For instance:
Code:
Transaction tx = session.beginTransaction();
Nomenclature nomenclature = ... // Gets the nomenclature
NomenclatureItem ni = (NomenclatureItem)nomenclature.remove(3);
// The size of the items is n-1 in memory
tx.commit();
...
nomenclature = ... // Gets the same nomenclature
System.out.println(nomenclature.getItems().size()); // will display n !!!
Is there a mistake in the above code or is it a normal feature of the persistent indexed List?
Thanks in advance.
MG