-->
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.  [ 3 posts ] 
Author Message
 Post subject: TransientObjectException
PostPosted: Thu Jan 20, 2005 6:33 pm 
Newbie

Joined: Mon Oct 25, 2004 12:53 pm
Posts: 9
Hibernate Gurus,

Child has reference to parent. Parent has reference to the child. Only child is persisted.

In a typical parent/child (bi-directional with inverse=true etc.) relationship, and in a master-detail web-page design, we load up a list of Parent.Id/Parent.col_x/Parent.col... (for display only on the master page) in one Session (then the session is closed). Using parent.id to link, we open up the detail page to maintain the Child. We get child info from JSP and start a new hibernate session to do session.saveOrUpdate(child) (//this is commented out - session.saveOrUpdate(parent);) to add this new child to Oracle.

Parent.id is a non-nullable FK in the Child, so we have to do a child.setParent(new Parent().setId(1)) before doing session.save(child). Since parent with id=1 already loaded (and detached) in session one, the new Parent does not match with that detached parent, so we get something like -

net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: pacakgeName.Parent

Without change to the hbm mappings, now we have two workarounds

parent = session.load(parent, 1); //another trip to DB, bad
child.setParent(parent);
session.saveOrUpdate(child);

or
parent = getCachedParentFromList(1); //we do not want to keep a huge List in MEM
child.setParent(parent);
session.saveOrUpdate(child);

I was wondering if there is better approach other than these two workarounds?
Thanks in advance!

JohnZ
jzhao@amfam.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 8:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Without change to the hbm mappings

Why without changes to the mapping file? The easiest way would be to enable cascades in the mapping file.

Quote:
parent = session.load(parent, 1); //another trip to DB, bad

Only another trip to the db if not defined as lazy...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 21, 2005 6:25 pm 
Newbie

Joined: Mon Oct 25, 2004 12:53 pm
Posts: 9
Thanks Steve,

Without change to the hbm mappings
Why without changes to the mapping file? The easiest way would be to enable cascades in the mapping file.

Here is 2-sides of the bi-directional mapping -

<!-- bi-directional one-to-many association to CwfWorkorder -->
<set name="cwfWorkorders" lazy="true" inverse="true" cascade="all-delete-orphan">
<key>
<column name="WO_STS_ID"/>
</key>
<one-to-many class="com.amfam.cwf.model.CwfWorkorder"/>
</set>


<!-- bi-directional many-to-one association to CwfWoStsCd -->
<many-to-one name="cwfWoStsCd" class="com.amfam.cwf.model.CwfWoStsCd" not-null="true">
<column name="WO_STS_ID"/>
</many-to-one>


parent = session.load(parent, 1); //another trip to DB, bad
Only another trip to the db if not defined as lazy...

Please see above mapping, we have lazy="true", but I see
another trip to DB from the log and a line like -
initializing non-lazy collections in the trace?

[DEBUG] done processing result set (1 rows) - [net.sf.hibernate.loader.Loader]
[DEBUG] done closing: 0 open PreparedStatements, 0 open ResultSets - [net.sf.hibernate.impl.BatcherImpl]
[DEBUG] closing statement - [net.sf.hibernate.impl.BatcherImpl]
[DEBUG] total objects hydrated: 1 - [net.sf.hibernate.loader.Loader]
[DEBUG] resolving associations for [com.amfam.cwf.model.CwfWoStsCd#1] - [net.sf.hibernate.impl.SessionImpl]
[DEBUG] creating collection wrapper:[com.amfam.cwf.model.CwfWoStsCd.cwfWorkorders#1] - [net.sf.hibernate.impl.SessionImpl]
[DEBUG] done materializing entity [com.amfam.cwf.model.CwfWoStsCd#1] - [net.sf.hibernate.impl.SessionImpl]
[DEBUG] running Session.finalize() - [net.sf.hibernate.impl.SessionImpl]
[DEBUG] initializing non-lazy collections - [net.sf.hibernate.impl.SessionImpl]

What's my problem, Steve?
Thanks a lot.
John


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