-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving associations..causes StaleObjectStateException
PostPosted: Wed Jan 18, 2006 3:09 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 4:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have inverse="true" in Parent's <set>. This means that saving Parent will only manage the relationship between Parent and all its Child objects: the Child objects themselves will not be saved. You have promised to manage Child objects yourself. If you don't want to do that (and if a Child can have only one Parent, then you probably don't), then delete the inverse attribute.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.