• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

about preUpdate and merge  RSS feed

 
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Himai Minh
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().

 
Himai Minh
Ranch Hand
Posts: 1703
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!