9.15.3 Order of invoking the callback methods
If multiple callback methods are defined for an entity, the invocation order follows the below rules:
-
The default listeners are invoked in the order defined in the O/R mapping file.
Unless @ExcludeDefaultListeners or the <exclude-default-listeners> tag of the O/R mapping file is explicitly specified, the default listeners are applied to all the entities in the persistence unit.
-
The callback methods are invoked in the order specified in @EntityListeners.
Note that if you use the O/R mapping file, you can execute the following operations:
-
Specifying the order of invoking the callback methods for the entities.
-
Overriding the order specified in the annotations.
-
-
The callback method specified in the entity (or mapped superclass) is invoked.
- Organization of this subsection
(1) Invocation order in the inheritance hierarchy
If the entity listener is defined multiple times in the inheritance hierarchy of the entity class and mapped superclass, the invocation order is as follows:
-
The default callback listener, if present, is invoked first.
-
The callback methods of the entity listener class are invoked sequentially from the listener specified in the superclass. At this time, if items such as @EntityListener are specified, that order is followed.
-
After the callback methods of all the entity listeners are invoked, the callback methods defined in the entity (or mapped superclass) are invoked sequentially from the listener specified in the superclass.
If the callback method is overridden in the subclass, the overridden method is not invoked. If the overridden callback method specifies different lifecycle events or if the overridden callback method is not a lifecycle callback method, the overridden method is invoked. Also, the callback method settings of the method are overridden.
(2) Excluding the callback methods
-
If you specify @ExcludeDefaultListeners or the <exclude-default-listeners> tag of the O/R mapping file, the default entity listener is not invoked in the entity class (or mapped superclass) and the subclasses.
-
If @ExcludeSuperclassListeners or the <exclude-superclass-listeners> tag of the O/R mapping file is applied to the entity class and mapped superclass, the listener callback method is not invoked in that class and the subclasses. @ExcludeSuperclassListeners or the <exclude-superclass-listeners> tag of the O/R mapping file does not exclude the invocation of the default entity listener.
-
If you use the O/R mapping file to explicitly specify the default or superclass listener excluded for the entity and mapped superclass, the default or superclass listener is applied to the entity and the subclasses.