JPA annotations, @PostUpdate etc

Ebean supports JPA annotations - @PostLoad, @PostPersist, @PostRemove, @PostUpdate, @PrePersist, @PreRemove, @PreUpdate


import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;


public class Customer {

  Long id;

  String name;

  Long version;

  public void prePersist1() {

  public void postPersist1() {

  public void preUpdate1() {

  public void postUpdate1() {

  public void preRemove1() {

  public void postRemove1() {

  public void postLoad1() {

Using the JPA annotations has some limitations such as it doesn't tell us which properties actually changed in an update and access to the underlying transaction etc.

When we hit these limitations we look to use BeanPersistController.


To listen for events on entity, we can also use the BeanPersistController and this gives us access to the underlying transaction as well as the specific properties changed in an update etc.

BeanPersistController is used to enhance or override the default bean persistence mechanism.

Object extraBeanToSave = ...;
Transaction t = request.getTransaction();
Database database = request.database();, t);


BeanPersistListener is different in two main ways from BeanPersistController postXXX methods.

BeanPersistListener only sees successfully committed events. BeanController pre and post methods occur before the commit or a rollback and as such these methods will see events that are later rolled back.

BeanPersistListener runs in a background thread and will not affect the response time of the actual persist where as BeanController methods will.


Use a BeanQueryAdapter to modify queries prior their execution. Typically, we add expressions to a query, for example to enable query partitioning based on the user executing the query.