Tony Lang
Tony Lang

Reputation: 170

What if entity listener method and lifecycle callback of the entity itself duplicates?

Just curious.

Saw the rule from WebSphere: Duplicate life cycle callback methods If multiple callback methods are defined for an entity life cycle event, the ordering of the invocation of these methods is as follows: life cycle callback methods defined in the entity listeners: The life cycle callback methods that are defined on the entity listener classes for an entity class are invoked in the same order as the specification of the entity listener classes in the EntityListeners annotation or the XML descriptor. Listener super class: Callback methods defined in the super class of the entity listener are invoked before the children. Entity life cycle methods: WebSphere® eXtreme Scale does not support entity inheritance, so the entity life cycle methods can only be defined in the entity class.

But it only mentioned the scenario multiple callbacks are from different listeners.

What if one callback (say @PostLoad) was defined in the entity class, but another (also @PostLoad) from the listener?

Which callback method would be invoked first? Is the sequence specified or provider-dependent?

Thanks a lot!

Upvotes: 0

Views: 649

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42114

Method in listener is called first, then callback method in entity is called. To be more specific, order is:

  1. Default listeners (ones defined in XML descriptor)
  2. Methods in listeners, starting from the superclasses in order they are defined in @EntityListeners annotation.
  3. Callback methods in entity hierarchy, starting from uppermost superclass.

Call order is specified in detail (and with clarifying examples) in JPA 2.0 specification, 3.5 Entity Listeners and Callback Methods.

Upvotes: 2

Related Questions