Hi,
I have a lot of bidirectional list associations following the pattern:
Code:
<!-- parent.hbm.xml -->
<list name="ChildType" inverse="true" table="CHILD" cascade="all" >
<key column="FK_ID_PARENT" />
<index column="INDEX_VALUE" type="Int32" />
<one-to-many class="Example.ChildType, Example" />
</list>
and:
Code:
<!-- child.hbm.xml -->
<many-to-one name="parent" class="Example.ParentType, Example" column="FK_ID_PARENT" />
In my test code I created children objects and added them to a parent object. The foreign key to the parent was stored in every child object (data base row), BUT NOT THE LIST INDEX.
After switching the mapping to
inverse="false" the list index was stored correctly in the children objects (data base rows).
It's somehow logical that
inverse="true" leads to this behavior because a child object does not know its index, i.e. its position in the list.
Some time ago I read somewhere in the docu (of NHibernate or Java-Hibernate) about a solution to this problem, but I couldn't find the place with this information again.
One possible (logical) solution I could imagine would be to:
1. define an instance variable in the child class,
2. map this variable to the index column and
3. set the variable's value (explicitly in the code) to the index value of the child object when the child object is added to the list.
But this is a bit cumbersome. Is there a simpler way to do it?
thanks,
mick