Hi.
I have those classes:
Code:
<hibernate-mapping>
<class name="my.faq.Topic" table="faq_topic">
<id name="id" column="ID" type="long" unsaved-value="any">
<generator class="hilo">
<param name="table">hibernate_unique_key</param>
<param name="column">next_faq_topic</param>
</generator>
</id>
<property name="name" column="name" type="string"/>
<list name="units" cascade="all" table="faq_unit" lazy="true">
<key column="topic_id"/>
<index column="index1"/>
<one-to-many class="my.faq.Unit"/>
</list>
</class>
<class name="my.faq.Unit" table="faq_unit">
<id name="id" column="ID" type="long" unsaved-value="any">
<generator class="hilo">
<param name="table">hibernate_unique_key</param>
<param name="column">next_faq_unit</param>
</generator>
</id>
<property name="topicId" column="topic_id" type="long"/>
<property name="index" column="index1" type="int"/>
</class>
</hibernate-mapping>
Now i do the following:
Code:
Unit firstUnit = new Unit();
Session sess = getNewSession();
Topic topic = sess.open(...);
topic.getUnits().add(firstUnit);
sess.update(topic);
System.out.println(firstUnit.getIndex()); // returns 0
sess.close();
<some other code>
Unit secondUnit = new Unit();
sess = getNewSession();
Topic topic = sess.open(...);
topic.getUnits().add(secondUnit);
sess.update(topic);
System.out.println(secondUnit.getIndex()); // returns also 0!
sess.update(secondUnit); // persists secondUnit with index=0
sess.close();
So, i get two objects persisted with index 0, and when i load topic, list contains only one of those objects.
If i won't update secondUnit directly and close the session, it gets index 1.
The question: is such behaviour ok, and i need to initialize lazy list before add object to it, or there is a some kind of bug?
Thanks in advance.
P.S. sorry for my english.