File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Entity callback doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Entity callback doubt" Watch "Entity callback doubt" New topic

Entity callback doubt

Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
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:

Shouldnt the postPersistAnimal callbacke be called first?
Suchitra Bhat
Ranch Hand

Joined: Dec 30, 2005
Posts: 62

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"
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Are callbacks in a listener treated differently or are listeners themselves treated differently for callbacks??
Ionut Bucurescu
Ranch Hand

Joined: Dec 19, 2006
Posts: 68

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.


SCJP 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA5
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Thanks Ionut,its clear now!!
I agree. Here's the link:
subject: Entity callback doubt
It's not a secret anymore!