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 ?
My name was Borat Sagdiyev. But the forum does not allow such names.
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.