Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

EJB 3, Merge operation question

Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For a given entity A, the MERGE operation behaves as follows: If A is a NEW entity, a new MANAGED entity A' is created and the state of A is COPIED into A'

So, in that case, merge behaves just like the persist operation ?, If so, is it a good practice to NEVER use the persist operation and instead always merge every instance without caring if it is new or if we are just updating the DB entity state ?

Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not the case....
Since the behavior of merge and persist differs for the operations of removed and detached entities.

For detached entities persist throws an EntityExistsException
Merge for detached entities copies and manages them and returns the managed instance

For removed entities persist - makes them managed again
Merge throws an IllegalArgumentException for removed entities.

So one should be careful what is the state of the entity which is being passed to either merge or persist operation for behaviors differ.

    Bookmark Topic Watch Topic
  • New Topic