public class EmployeeListener { @PrePersist public void prePersist(Object entity) { } } @Entity @EntityListeners({EmployeeListener.class}) public class Employee { @PrePersist public void prePersist() { } }
Default listeners (defined in an ORM xml):
<entity-mappings> <persistence-unit-metadata> <persistence-unit-defaults> <entity-listeners> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.DefaultListener"/> </entity-listeners> </persistence-unit-defaults> </persistence-unit-metadata> </entity-mappings>Default listener:
public class DefaultListener { @PrePersist public void prePersist(Object entity) { } }Managed class(entity and mapped-superclass) hierarchy:
Person | Employee / \ FullTimeEmployee PartTimeEmployee @MappedSuperclass @EntityListeners(PersonListener.class) abstract public class Person { @PrePersist public void verifyBirthday() { } } @Entity @EntityListeners({EmployeeListener.class, EmployeeListener2.class}) @ExcludeDefaultListeners abstract public class Employee extends EducatedPerson { @PreRemove private void preRemove() { } } @Entity @ExcludeSuperclassListeners @EntityListeners(FullTimeEmployeeListener.class) public class FullTimeEmployee extends Employee { @Override @PrePersist public void verifyBirthday() { } } @Entity public class PartTimeEmployee extends Employee { @PrePersist public void verifyHourlyWage() { } }Listener example:
public class PersonListener { @PrePersist public void prePersist(Person person) { } @PostPersist void postPersist(Person person) { } @PreUpdate protected void preUpdate(Person person) { } @PostUpdate private void postUpdate(Person person) { } @PreRemove private void preRemove(Person person) { } @PostRemove private void postRemove(Person person) { } @PostLoad private void postLoad(Person person) { } }When EntityManager calls persist(aFullTimeEmployee), the callback methods for prePersist event are called in the following order:
If a callback method throws an exception and entity manager is joined to transaction, the transaction will be marked for rollback only.
<entity-mappings> <persistence-unit-metadata> <persistence-unit-defaults> <entity-listeners> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.DefaultListener"/> </entity-listeners> </persistence-unit-defaults> </persistence-unit-metadata> <mapped-superclass class="com.cmobilecom.jpa.example.managed_classes.Person" > <entity-listeners> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.PersonListener"></entity-listener> </entity-listeners> </mapped-superclass> <entity class="com.cmobilecom.jpa.example.managed_classes.Employee" > <exclude-default-listeners /> <entity-listeners> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.EmployeeListener"></entity-listener> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.EmployeeListener2"></entity-listener> </entity-listeners> </entity> <entity class="com.cmobilecom.jpa.example.managed_classes.FullTimeEmployee" > <exclude-superclass-listeners /> <entity-listeners> <entity-listener class="com.cmobilecom.jpa.example.managed_classes.FullTimeEmployeeListener"></entity-listener> </entity-listeners> <pre-persist method-name="verifyBirthday"></pre-persist> </entity> <entity class="com.cmobilecom.jpa.example.managed_classes.PartTimeEmployee" > <pre-persist method-name="verifyHourlyWage"></pre-persist> </entity> </entity-mappings>