Event listening

...(under construction)...

Since version 4.0.1, Ebean supports JPA EntityListener 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;

...

@Entity
public class Customer {

  @Id
  Long id;

  String name;

  @Version
  Long version;


  @PrePersist
  public void prePersist1() {
      ...
  }


  @PostPersist
  public void postPersist1() {
    ...
  }


  @PreUpdate
  public void preUpdate1() {
    ...
  }

  @PostUpdate
  public void postUpdate1() {
    ...
  }


  @PreRemove
  public void preRemove1() {
    ...
  }

  @PostRemove
  public void postRemove1() {
    ...
  }

  @PostLoad
  public void postLoad1() {
    ...
  }

BeanPersistController

To listen for events on entity, you can also use the BeanPersistController

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

Note that if want to totally change the finding, you need to use a BeanQueryAdapter rather than using postLoad().

Note that getTransaction() on the PersistRequest returns the transaction used for the insert, update, delete or fetch. To explicitly use this same transaction you should use this transaction via methods on EbeanServer.

Object extaBeanToSave = ...;
Transaction t = request.getTransaction();
EbeanServer server = request.getEbeanServer();
server.save(extraBeanToSave, t);

It is worth noting that BeanPersistListener is different in three 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 will see events that are later rolled back

BeanPersistListener runs in a background thread and will not effect the response time of the actual persist where as BeanController code will BeanPersistListener can be notified of events from other servers in a cluster.

A BeanPersistController is either found automatically via class path search or can be added programmatically via ServerConfiguration.addEntity().