-->
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.  [ 5 posts ] 
Author Message
 Post subject: unneeded updates
PostPosted: Wed Jun 07, 2006 7:28 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
If I retrieve an object from the database and immediately save it, then should hibernate save the object? I set up a test to see if hibernate would avoid doing the update if nothing changed in the POJO. I'm doing this with two different DAO calls, hence there are two sessions. One to retrieve the object and another to save the object.

Can anyone tell me if there is a way to prevent hibernate from saving the POJO if nothing changed?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 7:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're using a session that didn't load the object, how can it know if the object is different from the database version, or not? It has to save the object, because it doesn't know if it can take shortcuts.

You could use session.refresh or session.lock before saving the object. That will issue a select, and now hibernate knows whether or not the object is dirty, and can avoid saving it if possible.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 9:21 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Seems the way around this is to do a select-before-update="true". I've tried using session.lock and session.refresh but it doesn't work. Can I get anyone elses opinions or experiences on this?

thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 9:28 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
I would avoid the save step. If you retreive an object in a current Session, Hibernate is tracking that object as a managed object and tracks changes to it.

When your transaction commits, Hibernate will flush changes to the database. At this point if you have made modifications Hibernate will issue an update statement, if not then it will not issue an update.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 9:52 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
He's using two sessions: the one that's saving is not the one that loaded the object. Your statement is correct but not applicable in this case.

_________________
Code tags are your friend. Know them and use them.


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