File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to understand session.merge()

 
john sal
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the post
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic