-->
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: what is session merge() ?
PostPosted: Fri Aug 27, 2010 11:37 am 
Newbie

Joined: Fri Oct 07, 2005 12:34 am
Posts: 5
I need help to understand session merge() in hibernate. After hours of search in net and books , I am frustrated to understand this .

I have gathered some information though.

merge is used for detached objects .i.e objects which are not attached to DB and disconnected hibernate session.

Now , if we modify the POJO (modifying fields using setter methods) and then again we want to save this POJO , then we need to create a new hibernate session....now do we call merge here or update here ? here I am stuck .

what merge will do ?

what update will do ?

Could you show me a demo code also to use this feature.

This looks complicated to me....I'm very much upset with this concept.


Top
 Profile  
 
 Post subject: Re: what is session merge() ?
PostPosted: Sat Aug 28, 2010 6:46 am 
Newbie

Joined: Sat Aug 28, 2010 6:27 am
Posts: 1
One correction first: Merge is also used for transient instances (Instances which haven't been saved at all yet).

When you have a new hibernate session with detached instance of object A, it doesn't matter in most cases if you use merge() or update() to save the changes. So in your example, calling merge() or update() would yield the exact same result.

However, if you have two objects with the same identifiers where one of them is detached and the other one is persistent, calling update() on the detached instance will throw an exception where merge will save the changes.

Oh, and one more thing. Merge() saves the _STATE_ of the object, so the member variables are saved to the db and the function returns the persistent instance while update() will save the object itself and make it persistent.

Hope this helps :)


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.