Hitachi

uCosminexus Application Server Compatibility Guide


9.15.1 Location for specifying the callback method

You can specify the callback method at the following locations:

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.

Table 9‒18: Specifying the callback method using annotations

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: