Callback Events
A certain annotation represents each phase of an entity's life cycle:
@javax.persistence.PrePersist
@javax.persistence.PostPersist
@javax.persistence.PostLoad
@javax.persistence.PreUpdate
@javax.persistence.PostUpdate
@javax.persistence.PreRemove
@javax.persistence.PostRemove
The @PrePersist
and @PostPersist
events have to do with the insertion of an entity instance into the database. The @PrePersist event occurs immediately when the EntityManager.persist( ) call is invoked or whenever an entity instance is scheduled to be inserted into the database (as with a cascaded merge). The @PostPersist event is not triggered until the actual database insert.
The @PreUpdate
event is triggered just before the state of the entity is synchronized with the database. The @PostUpdate
event happens after. This synchronization could occur at transaction commit time, when EntityManager.flush( ) is executed, or whenever the persistence context deems it necessary to update the database.
The @PreRemove
and @PostRemove
events have to do with the removal of an entity bean from the database. @PreRemove is triggered whenever EntityManager.remove( ) is invoked on the entity bean, directly or because of a cascade. The @PostRemove event happens immediately after the actual database delete occurs.
The @PostLoad
event is triggered after an entity instance has been loaded by a find( ) or getreference( ) method call on the EntityManager interface, or when an EJB QL query is executed. It is also called after the refresh( ) method is invoked.
 |