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: Flush vs SaveOrUpdate
PostPosted: Thu Nov 08, 2007 10:40 am 
Newbie

Joined: Wed Apr 04, 2007 9:40 am
Posts: 12
Ho boy, I bet this question has been answered millions of times, but searching the forums turned up so many irrelevant posts that I gave up.

Basically, when should I use SaveOrUpdate versus Flush?

When I create a new object, I commit it to the database with SaveOrUpdate.
When I update an object, do I use Flush or SaveOrUpdate?

So far I had been using SaveOrUpdate most of the time, but I am worried it is wrong.

Also, when is it pertinent to do a Flush? I'm using NH in a web application and I was wondering if doing a single Flush just at the end of the page processing would be sufficient or there would be situations where it would cause a problem?

Thanks for your help. I am trying to improve the performance of my stuff and I'd like to learn some best practices at the same time.

Alain-Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Flush() and SaveOrUpdate() are different concepts.

Use Update() if the entity is a detached object; i.e. the object is not associated with the session anymore. That means it one of three things: the object has been loaded from a different session; you have cleared the session; or you have evicted the object from the session. Update() or Lock() to re-associate the object with the session.

Use Save() if the entity is a transient object; i.e. the object is brand spanking new.

Use SaveOrUpdate() if you don't know if the object is new or not.

In all of the above cases, you may still need to call Flush(). Flush() basically synchronize the session with the database. In other words, Flush() flushes any pending operation that has not been pushed to the database.

If you use transactions, you almost never need to call flush as it is automatically called on commit.

_________________
Karl Chu


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.