9.15.2 Implementing the callback methods
The user implements the callback method as and when required. The callback method signature differs in the callback method implemented in the entity class and mapped superclass and in the callback method of the entity listener class.
The callback method defined in the entity class and mapped superclass has the following signature:
void <METHOD>
The callback method defined in the entity listener class has the following signature:
void <METHOD>(Object)
In the argument Object, you specify the entity instance in which the callback method is executed.
- Organization of this subsection
(1) Notes on using the callback methods
Note the following regarding the callback methods. If the following conditions are not satisfied, an exception occurs at application startup and the application fails to start:
-
A constructor without argument must be specified in public.
-
The public, private, protected, and package level access is allowed with the callback method. However, static and final are not available.
-
One class cannot have multiple lifecycle callback methods for the same lifecycle event. However, the same method might be used in multiple callback events.
(2) Rules applied to the callback methods
The following rules are applied to the callback methods:
-
With the callback method, the issuing of unchecked or runtime exceptions are allowed. The runtime exception thrown by the callback method executed in the transaction rolls back the transaction. If multiple callback methods are specified, after the runtime exception is thrown, the remaining callback methods are not executed.
-
With the callback method, you can execute JNDI, JDBC, JMS, and Enterprise Bean.
-
Do not perform the following operations with a callback method:
-
Invoking EntityManager.
-
Executing a query operation.
-
Accessing other entity instances.
-
Updating a relationship.
If a callback method is used for such methods, the operations might not function properly.
-
-
If the callback method is invoked in the Java EE environment, the entity callback listener shares the naming context of the components to be invoked. Furthermore, the callback method of the entity is invoked in the transaction and in the security context of the invocation source components used when the callback method is invoked.