Pg 55 EJB Persistence spec 3.4.4 Multiple Lifecycle Callback Methods for an Entity Lifecycle Event
"If a lifecycle callback method for the same lifecycle event is also specified on the entity class and/or one or more of its entity superclasses,the callback methods on the entity class and/or entity superclasses are invoked after the other lifecycle callbackmethods,most general superclass first."
And in 3.4.5 example the output is as: postPersistPetListenerMethod postPersistCatListenerMethod postPersistCatListener2Method postPersistAnimal
Shouldnt the postPersistAnimal callbacke be called first?
I was also confused about this. I understood as below.
Entity Animal does not have EntityListner associated with it. Entity pet has PetListner.class as EntityListner Cat has two EntityListners in the order CatListener.class and CatListener2.class
So the flow is like this When @PostPersist event occurs on an instance of cat
the most general entity listener is PetListner and then are CatListener and CatListener2 and then comes the most general super class and then subclasses of it.
The same thing i found in Mikalais notes too. It says " Entity listener method are invoked in a specific order when a given event occurs . Default listeners first if any , EntityListeners are invoked in the inheritance hierarchy and then super class listeners being invoked before subclass listeners"
In ejb-3_0-fr-spec-ejbcore.pdf section 12.4.1 Multiple Callback Interceptor Methods for a Life Cycle Callback Event you can find the text:
"If there are any interceptor classes defined on the bean class, the lifecycle callback interceptor methods defined by those interceptor classes are invoked before any lifecycle callback interceptor methods defined on the bean class itself."
So any lifecycle callback interceptor defined in a separate class, not in the bean class, are called first. The superclasses external interceptors are called first.
That's why postPersistPetListenerMethod is called first because the Pet class is the first superclass which defines external interceptors.
postPersistAnimal is called last because is the only lifecycle callback defined inside the bean in the hierarchy.