×
☰ See All Chapters

JPA Callback Methods

JPA provides callback methods for monitoring changes in the lifecycle of persistent objects. Using these callback methods we can perform various actions at different stages of a persistent object's lifecycle. Below is the list of lifecycle callback methods:

PrePersist: Methods annotated with this annotation will be invoked before persistence object is persisted.

PostPersist: Methods annotated with this annotation will be invoked after persistence object has transitioned to the persistent state. PostLoad: Methods annotated with this annotation will be invoked after all eagerly fetched fields of your persistence class have been loaded from the database.

PreUpdate: Methods annotated with this annotation will be invoked just the persistent values in persistence objects are flushed to the database. Please note the values are just flushed and not committed.

PostUpdate: Methods annotated with this annotation will be invoked after changes to a given instance have been committed to the database.

PreRemove: Methods annotated with this annotation will be invoked before executing delete transactions.

PostRemove: Methods annotated with this annotation will be invoked after persistence object has been marked as to be deleted.

How to use Callback Methods       

  1. Callback methods on a persistent class should be no argument method and should return void. 

  2. Multiple events can be assigned to a single method as well. 

  3. Below is an example of how to declare callback methods on persistent classes: 

@Entity

public class Employee {

        @Transient

        private String fullName;

       

        private String firstName;

       

        private String lastName;

       

        @ManyToMany

        private List<PhoneNumber> phoneNumber;

 

        @PostLoad

        public void setFullName() {

                fullName = firstName + lastName;

        }

       

        @PreDelete

        public void studentDeletion() {

                log.info("deleting student whoes full name is" + fullName );

        }

}

 

Using Entity Listeners

Writing callback methods into your persistent classes is not always good practice. It is good to handle lifecycle events in a non-persistent listener class. JPA provides an option to do this in a listener classes.

  1. Listener classes should have a default constructor.  

  2. Listener classes can have any number of callback methods.  

  3. The callback methods must take in a single java.lang.Object argument which represents the persistent object that triggered the event. 

  4. Entities can use listeners using the EntityListeners annotation. This annotation takes an array of listener classes as its value. 

Below is an example of how to declare an entity and its corresponding listener classes.

 

@Entity

@EntityListeners({ EmployeeLogger.class, …})

public class Employee {

        // ... //

}

 

public class EmployeeLogger {

        @PostLoad

        public void setFullName(Object obj) {

                obj.fullName = (String)obj.firstName + (String)obj.lastName;

        }

       

        @PreDelete

        public void studentDeletion(Object obj) {

                log.info("deleting student whoes full name is" + obj.fullName );

        }

}

 

 


All Chapters
Author