Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
You can designate an entity listener method on an entity listener class of a JPA entity as a life cycle callback method.
To configure a life cycle callback listener method on an entity listener class, you must do the following:
Create an entity listener class.
This can be any POJO class.
Implement the life cycle callback listener method in the entity listener class.
Callback methods defined on a JPA entity listener class have the following signature:
void <METHOD>(Object)
You may specify an argument type of Object
or the type of the JPA entity class that you will associate the entity listener class with.
Associate a life cycle event with the callback listener method.
You may associate a life cycle event with one and only one callback listener method, but you may associate a given callback listener method with more than one life cycle event.
For more information, see the following:
Associate the interceptor class with your JPA entity.
For more information, see the following:
For more information, see the following:
You can specify a JPA entity listener method as a life cycle callback method using any of the following annotations:
Example 7-31 shows how to use the @PostConstruct
and @PreDestroy
annotation to specify JPA entity listener methods myPostConstruct
and myPreDestroy
as life cycle callback methods, respectively.
Example 7-31 @PrePersist Life Cycle Listener Callback Method
public class MyProjectEntityListener { ... @PostConstruct public void myPostConstruct (Project obj) { // or just Object ... } @PreDestroy public void myPreDestroy (Project obj) { // or just Object ... } }
You can associate an entity listener class with a JPA entity using the @EntityListeners
annotation. Example 7-32 shows how to associate the entity listener class from Example 7-31 with a JPA entity class.
Note that the life cycle method for @PrePersist
is a method of the JPA entity class itself (for more information, see "Configuring a Life Cycle Callback Method on a JPA Entity").
Example 7-32 Associating an Entity Listener Class With a JPA Entity
@Entity @EntityListeners(MyProjectEntityListener.class) @Table(name="EJB_PROJECT") public class Project implements Serializable { ... @Id @Column(name="PROJECT_ID", primaryKey=true) public Integer getId() { return id; } ... @PrePersist public int initialize() { ... } }