Home > database >  When does PostUpdateEventListener get called when using @Transactional
When does PostUpdateEventListener get called when using @Transactional

Time:08-14

I'm trying to figure out when PostUpdateEventListener is called when encapsulating the method with @Transactional. Let's say we have Entity X, and where the @Transactional method (Class A) calls another class (Class B) with the @Transactional method. Class B makes no modifications to Entity X, while Class A makes modifications before and after the call. When doing this I've noticed when debugging that custom-defined PostUpdateEventListener will get called immediately when the call is made to Class B's method, and also again at the end of Class A method. I've been trying to dig into how and when to expect this listener to get called, any help would be appreciated.

CodePudding user response:

Insert, update or remove listeners are called before or after the actual flush. If you have an entity in your first-level/persistence context that is dirty (i.e. you changed data on it), then Hibernate will flush changes as late as possible, the latest point being before transaction commit. If you query something, that reads from a table which would be affected by a flush, then Hibernate forcefully does that flush before executing the query and hence also executes certain listeners as part of that flushing.

  • Related