This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Usage of entity manager's merge() in case of managed entities

 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I have a query related to the usage of entity manager's merge() operation.
I have come across lots of code where entities are being fetched using find and inside transactions. All the code which modifies these fetched entities as well is being executed inside the scope of transactions. And the entities are being updated with calls to em.merge().
Isn't this an avoidable call? Since the entity find() is happening inside a transaction, it will be in the scope of persistence context and any modifications to the entity should be visible to the persistence context. And the persistence provider (I am using hibernate) should commit these changes to the database irrespective of whether merge() is invoked or not? Is my understanding correct?

The spec says "If X is a managed entity, it is ignored by the merge operation, however, the merge operation is
cascaded to entities referenced by relationships from X if these relationships have been annotated
with the cascade element value cascade=MERGE or cascade=ALL annotation.".
So, I hope I can safely remove all the merge() calls being made.
Also wondering, If the merge() calls are left as such, would they have any impact on performance?

Another question on the statement from the spec is - If X is a managed entity, the entities reference by X should also have been managed.
So, merge operation cascaded from X should also be ignored on the referenced entities. Is that correct?

Thanks in advance for any clarifications.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct - if the entities are retrieved within a valid transaction then any changes to those objects will be persisted when the transactions ends.

You should safely be able to remove those calls, but make sure you test all the functionality once removed as transaction configuration can often be done incorrectly, and if explicit merge calls were being used you may never have known.

You should remove them as this isn't really what the merge() functionality was designed for - its purpose was to handle merging of detached entities. Care should be taken when using merge - and if you wish to continue using the entity after merging you should make sure you reset it:

x = em.merge(x);


if you don't then x will often be unusable after merge has occured and you will likely see strange results.
 
You have to be odd to be #1 - Seuss. An odd little ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic