-->
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: Best method to reattatch an object to session?
PostPosted: Thu Feb 05, 2009 4:51 pm 
Newbie

Joined: Tue Oct 28, 2008 8:10 am
Posts: 5
Hello,

I use hibernate/tapestry and have objects stored in an application state object but when I try to access children, i get the lazy load exception.

People are suggestion to basically use one of 3 methods to reattatch the object to the session before calling for children objects:

session.refresh(Object) (overrides the object if there has been changes maybe?
session.update(Object) (saves the object, does it save if there has been no changes?)
session.lock(Object,LockMode.NONE)(looks like it does basically nothing so maybe the best option?)

I have tried all 3 and they work but which if any is the best?




--James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 5:59 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If you just want to do it to avoid lazy initialization exceptions, use session.lock(). It has no overhead and generates no database access. But, if your object has changed, this will not be detected by Hibernate and the changes are not stored in the database.

session.refresh() reloads the object from the database and if you have changed anything those changes are lost.

session.update() will update the database. Hibernate assumes that you are calling this method because you have modified it and want to save the changes to the database.

The tricky thing with having objects outside the session is that Hibernates automatic dirty checking mechanism no longer work. So, you need to keep track of changes yourself and call the appropriate method. There is also the session.merge() method which like a combination of session.refresh() and session.update() or session.lock(), except that the original object passed to merge() doesn't become attached to the session. The merge() method instead returns a new object (assuming that the entity really is detached and isn't already in the session).


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.