I have a simple parent object with one-to-many r/n with child objects...and here is how the mapping looks like..
Parent.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sf.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Parent" table="PARENT">
<id name="draftId" type="string">
<column name="DRAFT_ID" length="17" />
<generator class="assigned" />
</id>
<version column="VERSION" name="version" unsaved-value="negative"/>
<property name="docId" type="string">
<column name="DOC_ID" length="17" />
</property>
<property name="batchId" type="string">
<column name="BATCH_ID" length="12" />
</property>
<set name="children" inverse="true" cascade="all">
<key>
<column name="DRAFT_ID" length="17" not-null="true" />
</key>
<one-to-many class="child" />
</set>
</class>
</hibernate-mapping>
Child.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sf.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="child" table="CHILD">
<id name="id" type="long">
<column name="ID" length="5" />
<generator class="assigned" />
</id>
<version column="VERSION" name="version" unsaved-value="negative"/>
<property name="description" type="string">
<column name="DESCRIPTION" length="30" />
</property>
<property name="comments" type="string">
<column name="COMMENTS" length="30" />
</property>
<many-to-one name="draftId" class="Parent" column="draft_id" cascade="all" />
</class>
</hibernate-mapping>
And in the DAO, I create new parent and child objects and save just the parent by the following code..
Parent p = new Parent(id=myID, ...); // calling constructor
Child c= new Child(..);// calling child's constructor.
p.addChild(c);// establishes the association
default child's version = -1;
// create hibernate sessionFactory/session
session.save(p);
Executing above code, calls insert on Parent object, but update on child object... and this throws StaleObjectStateException. My question is why is update getting invoked on child when I am expecting insert with the following config(child.hbm.xml): <version column="VERSION" name="version" unsaved-value="negative"/> (I am defaulting the version to -1 in the child object).
Thanks in advance.
|