This week's book giveaway is in the Features new in Java 9 forum.
We're giving away four copies of Java 9 Revealed and have Kishori Sharan on-line!
See this thread for details.
Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB 3, Merge operation question  RSS feed

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

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 ?

Thanks,
 
Shivani Chandna
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.

Regards,
Shivani.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!