I've got an odd problem where data in a List that was once persisted gets nulled out on an 'update' operation.
For a trivial example, class T1 has a one-to-many relationship with T2, implemented thus:
Code:
T1 t1 = new T1(); //T1's constructor creates a list and adds a T2 to it
t1.nextT2(); //creates another T2 object and appends it to the list;
t1.nextT2();
...etc
here's how they are mapped:
Code:
<class name="T1" table="t1">
<id name="id" column="t1_id" type="long">
<generator class="sequence">
<param name="sequence">t1_seq</param>
</generator>
</id>
<list name="t2s" table="t2" cascade="all">
<key column="t1_id"/>
<index column="idx"/>
<one-to-many class="T2"/>
</list>
</class>
<class name="T2" table="t2">
<id name="id" column="t2_id" type="long">
<generator class="sequence">
<param name="sequence">t2_seq</param>
</generator>
</id>
<property name="t1Id" column="t1_id" type="long"/>
</class>
My problem is that if I do this:
Code:
Session sess = HibernateUtil.getSession();
T1 t1 = new T1();
t1.nextT2();
sess.save(t1);
t1.nextT2();
t1.nextT2();
t1.nextT2();
sess.update(t1);
What I end up with are 5 new entries in the t2 table, with the first two (at idx 0 & 1) having NULL in the t1_id column. The last three rows (indices 2-4) are populated with the t1_id as they should be.
I've written a test cast which exposes this behavior, and have verified that after the 'save()' and before the 'update()', the t1_id exists for all the new t2 entries. It's only when updating the t1 object that its t2 references which existed prior have their t1_id field nulled out.
Is this a problem with my cascade setting? I've verified this behavior is database independent, as I can produce it in Oracle and PostgreSQL. I'm using Hibernate v. 3.2
I've got the mapping files, the classes, and the test case in an archive and can give it via email if anyone wants to try to reproduce this error. Any help is much appreciated!!!
--Kris