wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Need to understand session.merge() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Need to understand session.merge()" Watch "Need to understand session.merge()" New topic
Author

Need to understand session.merge()

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
I want to understand how session.merge() works for the below scenarios:

There is a detached instance of a database entity that is modified after detachment from the previous session. Now on trying to re-attach this instance with a new session using session.merge()

1) If the persistence instance does not exist in the session, what will be the behavior, the Api says we'll get a new persistent instance and the detached object will be discarded, whereas I see that the detached instance is not discarded. The new instance retrieved after the merge has the modifications from the detached instance.

2) Before session.merge(), there is a dirty persistence object for the same id. After merge, what will happen to the modifications done on persistent object before the merge operation, will they remain or they'll get discarded?

Thanks!
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
1) If the persistence instance does not exist in the session, what will be the behavior, the Api says we'll get a new persistent instance and the detached object will be discarded, whereas I see that the detached instance is not discarded. The new instance retrieved after the merge has the modifications from the detached instance.

The detached object won't be discarded after merging. It will remain detached i.e. it won't be associated with the session. The merge method will detect whether an update is required and then load a persistent object using the id of the detached object. This gives the impression that the detached object has automatically been re-attached to the session. Like all Java objects, the detached object will be discarded when the application no longer holds a reference to it.

2) Before session.merge(), there is a dirty persistence object for the same id. After merge, what will happen to the modifications done on persistent object before the merge operation, will they remain or they'll get discarded?

Modifications made to the persistent object before merging will be overridden. The state of the detached object will be copied onto the persistent object. In other words, there will be no difference between the state of the detached object and state of the persistent object.
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
Thanks for the post
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to understand session.merge()
 
Similar Threads
How do you update a collection if some exist and some are new?
problem with find() in jpa
Differences among save,update,saveorupdate,merge method in session object?
Differences among save,update,saveorupdate,merge method in session object?
New entity instances and EntityManager.merge() operation