Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Need to understand session.merge()  RSS feed

 
Ranch Hand
Posts: 94
  • 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!
 
Ranch Hand
Posts: 180
  • Likes 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the post
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!