-->
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.  [ 13 posts ] 
Author Message
 Post subject: Saving associations..calling insert on parent only..
PostPosted: Wed Jan 18, 2006 3:07 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 3:22 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
because cascade=all and may be you are missing an id in the Child class.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 4:03 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
Thanks LaLiLuna.
I am setting the Id on the child as well. Id on child does not seem to be the issue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 6:26 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Isn't there another topic about this? It's because of inverse="true".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 10:57 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
I tried turning off inverse=true...i.e., took that attribute out and still ending up with the same effect. I suspect unsaved-value attribute is not getting set..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:02 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
I tried turning off inverse=true...i.e., took that attribute out and still ending up with the same effect. I suspect unsaved-value attribute is not getting set..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 11:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's easy to test. Remove the version element temporarily, see if it works without it.

I've never encountered a bidrectional mapping with cascades going in both directions. I'm sure it works, but maybe it doesn't, and that might be the problem? Are you sure that you want to cascade in both directions? You probably want to remove the cascade from the many-to-one, and leave it only in the set.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 4:32 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
could you verify that all types are properly mapped
integer to integer and not to long

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 7:06 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
I dont really need casade bidirectional. I now took out cascade=all from the child.hbm.xml. But it still does not help....update is getting invoked on child instead of insert...what could be the bugger??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 7:26 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
I also took the version element per your suggestion, and even this time update is getting called but with no StaleObjectStateException...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 8:59 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think you'll have to debug it to figure it out. The first thing I'd try would be to Session.get the child with the id that you're about to save, just to verify that the Session hasn't somehow already got it in its cache. After that, I'd put Session.flush after every DB interaction, even the selects/queries/gets/loads... And finally, I'd resort to using an actually debugger and stepping into hibernate code to find out why it's choosing update instead of save.

BTW in an earlier post you said I suspect unsaved-value attribute is not getting set. I notice that there is no unsaved-value attribute on the id, which I think is the important one. Unsaved-value on a version tag just means that the value in the version column is to be created rather than re-saved; you need an unsaved-value on the id to signify that an object needs an insert rather than an update.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 9:46 pm 
Newbie

Joined: Wed Jan 18, 2006 2:46 pm
Posts: 8
Thanks Tenwit for your patience!

In my example there is just one db interaction...so, just one flush. Not sure how to debug into hibernate code...am a newbie to java aswell..am from c++ background.

I thought unsaved-value on <version> has the same effect...as its effect on<id>(or am I wrong). Moreover I thought unsaved-value can go with <id> only if its value is generated(thru sequence/other algrthm)..not when its value is assigned. ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It looks like you're right:

Choosing the assigned generator makes Hibernate use unsaved-value="undefined", forcing Hibernate to go to the database to determine if an instance is transient or detached, unless there is a version or timestamp property, or you define Interceptor.isUnsaved().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.