9.15.1 Location for specifying the callback method
You can specify the callback method at the following locations:
-
In the entity class or mapped superclass
-
Entity listener class associated with the entity class or mapped superclass
The entity listener class is a dedicated class for implementing the callback method. If you use the entity listener class, you can separate the parts for implementing the callback method.
Specify the callback method in an annotation or the O/R mapping file. However, the default callback method is specified in the O/R mapping file and cannot be specified in an annotation. Note that the default callback method indicates an entity listener applied to all the entities in a persistence unit.
This section describes how to specify the callback listener.
- Organization of this subsection
(1) Specifying the callback method in an annotation
If you use an annotation for specifying the callback method, set the annotations listed and described in the following table in the method. You can invoke the method in compliance with a lifecycle event.
Annotation |
Executed contents |
---|---|
@PostLoad |
The callback method is executed after the entity is loaded in the persistence context or after the refresh operation is applied. The method is executed after the entity is read from the cache or the SELECT statement is issued for the database. |
@PrePersist @PreRemove |
The callback method is invoked before EntityManager executes the persist or remove operation of the entity. When the merge operation is applied and a new managed instance is created, the PrePersist callback method is invoked for the managed instance after the entity state is copied. The PrePersist or PreRemove callback methods are also invoked for all the instances where the operations are cascaded. The PrePersist or PreRemove method is always invoked synchronously as a part of the persist, merge, or remove operations. |
@PostPersist @PostRemove |
The PostPersist and PostRemove callback methods are invoked after the entity is perpetuated or deleted by the persist or remove operations. These callback methods are also invoked for all the entities where the operations are cascaded. The PostPersist or PostRemove methods are respectively invoked after the database insert or delete operations are performed. These database operations are executed immediately after the persist, merge, or remove operations or after the flush method is invoked. However, the database operations may also be executed at the end of a transaction. The generated primary key can be used with the PostPersist method. |
@PreUpdate @PostUpdate |
The PreUpdate and PostUpdate callback methods are respectively invoked before and after the database update operation of the entity data. These database operations are executed when the entity state is updated or when the state is flushed in the database. However, the database operations might also be executed at the end of a transaction. When an entity is perpetuated and then updated and when an entity is updated and then deleted in one transaction, the PreUpdate and PostUpdate callback might not occur. |
To use the entity listener class, you must specify the entity listener class by specifying @EntityListener for the entity. The following is an example of specifying an entity listener class:
@Entity @EntityListeners(CallbackListener.class) public class Employee implements Serializable{ ... }
(2) Specifying the callback listener in the O/R mapping file
Specify the following settings to use the O/R mapping file for specifying the callback method:
-
To specify the entity listener class and the callback method of that class, use the <entity-listener> tag of the O/R mapping file. Specify the lifecycle listener method by using the <pre-persist> tag, <post-persist> tag, <pre-remove> tag, <post-remove> tag, <pre-update> tag, <post-update> tag, and <post-load> tag beneath the <entity-listener> tag.
-
When you specify the callback method of the entity listener class, you can specify maximum one method for each callback event by using the tags beneath the <entity-listener> tag.
-
If you specify the <entity-listener> tag of the O/R mapping file for the lower tags of the <entity-listeners> tag in the <persistence-unit-defaults> tag, you can specify the default callback method.
-
If you specify the <entity-listener> tag in the lower tags of the <entity-listeners> tag existing in the <entity> tag or <mapped-subclass> tag, the callback listener is specified for the entity or mapped superclass and the subclasses.
-
The callback listener is invoked in the order of listeners specified in the <entity-listeners> tag. For details on the order for invoking the listeners, see 9.15.3 Order of invoking the callback methods.