Hi, for everyone's reference,
I came across a question from Enthuware. It talks about how life cycle callback method is related to merge.
I modified the chapter 12 of JPA Pro 2, lifecycle example. This is what I found out:
I first create employee id =1 for John. Then, I call modifyEmployee(). The new entity ( id=1, name=Bob) is merged with John.
John is changed into Bob.
Pre update and post update are called when modifyEmployee() is called.
I clear all tables in the DB.
I create employee id=1 for John and then I call modifiedManagedEmployee. I got the same resume.
Pre update and post update are called.
The interesting thing happens in modifyEmployee(). Bob is a new unmanaged entity and it is being merged with John with the same id=1.
Pre/post update is called.
I expect pre/post persist is called because Bob is a new and unmanged entity in the persistence context. I also expect the transaction will fail as Bob is intended to be persisted.
But it turns out that Bob is merged and pre/post update is called.
posted 1 year ago
One interesting note is that :
when I call modifyEmployee with a new id such as (id=2, Bob) and merge.
Bob does not exist in the database, the merge creates a new entity and persists Bob as a new entity.
Pre/post persist methods are called.
This matches with what the spec says that pre persist and post persist may be call for merge().
posted 1 year ago
One more experiment:
Only pre/post persist are called. But the pre/post udpate are not called.
That matches with what the specification says. It says in one transaction, an entity is persisted and then modified, whether pre/post udpate is called is vendor specific.
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database