Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javax.persistence.CascadeType.MERGE - meaning of MERGE ?

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the classed Exam (Id, ExamName, Detail_Id) and ExamDetail(Detail_Id, numOfQuestions, maxPoints, isObjective). Exam HAS-A or CONTAINS A ExamDetail.
My book says that MERGE ensures that when an encapsulating instance, such as the Exam, is reattached to its persistent storage, any changes or updates to the encapsulated
class, i.e ExamDetail, would be merged as well.

What is the meaning of "reattached" ? How does MERGE work ?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali,
When you get a JPA object out of the database, it can be attached or unattached. Attached means changes you make to the object are updated in the database (when you commit/close the connection). Unattached means the database doesn't know about those changes.

For example, suppose I pull your user record and store it in your session when you post. I want to disconnect because I don't want the database connection to be busy while you post. You might get coffee and be slow. Or decide not to post. When you do post, I change your last post date. But the entity is now disconnected/unattached. I need to re-attach.

If I'm the only one who made changes, the merge gets them. If the database has changes too, the ORM implementation (like Hibernate) needs to figure out what to do.

For more, see this explanation of detached entities
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic