September 2010
Intermediate to advanced
766 pages
18h 35m
English
You can have an entity bean instance register for a callback on
any of these lifecycle events by annotating a public, private, protected,
or package-protected method on the bean class. This method must return
void, throw no checked exceptions, and
have no arguments:
import javax.persistence.Entity; import javax.persistence.PostPersist; import javax.persistence.PrePersist; /** * Represents an Employee which is able to receive JPA * events. */ @Entity public class EntityListenerEmployee { private String name; public String getName() { return name; } public void setName(final String name) { this.name = name; } /* * Event Listeners; fired by JPA and track state in the EventTracker */ @PrePersist @SuppressWarnings("unused") private void prePersist(){ ... } @PostPersist @SuppressWarnings("unused") private void postPersist(){ ... } ... }
When an event is triggered on a particular managed entity instance, the entity manager will invoke the appropriate annotated method on the entity bean class.