Close

JPA - Defining EntityListener class by using @EntityListeners

[Updated: Nov 27, 2018, Created: Jan 21, 2018]

This example shows how to use a separate Listener class by using @EntityListeners. In this case, the method annotated with lifecycle annotations (as listed here) should take one argument. This one argument should be the entity type which we are interested in receiving notification for. This argument type can also be entity's superclass or an implementing interface. The target entity must also use @EntityListeners annotation on the class level to define what listener classes to receive notifications.

Example

The Entity

@Entity
@EntityListeners(MyEntityListener.class)
public class MyEntity {
  @Id
  @GeneratedValue
  private int id;
  private String msg;

  public MyEntity() { }

  public MyEntity(String msg) {
      this.msg = msg;
  }
    .............
}

The Listener

public class MyEntityListener {

  @PrePersist
  void onPrePersist(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPrePersist(): " + myEntity);
  }

  @PostPersist
  void onPostPersist(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostPersist(): " + myEntity);
  }

  @PostLoad
  void onPostLoad(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostLoad(): " + myEntity);
  }

  @PreUpdate
  void onPreUpdate(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPreUpdate(): " + myEntity);
  }

  @PostUpdate
  void onPostUpdate(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostUpdate(): " + myEntity);
  }

  @PreRemove
  void onPreRemove(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPreRemove(): " + myEntity);
  }

  @PostRemove
  void onPostRemove(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostRemove(): " + myEntity);
  }
}

Performing Entity Operations

public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEntity();
          loadAndUpdateEntity();
          mergeEntity();
          loadAndRemoveEntity();
      } finally {
          entityManagerFactory.close();
      }
  }

  public static void persistEntity() {
      System.out.println("-- persisting --");
      MyEntity myEntity = new MyEntity("test msg");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      System.out.println("before EntityManager.persist()");
      em.persist(myEntity);
      System.out.println("after EntityManager.persist()");
      System.out.println("before EntityManager.commit()");
      em.getTransaction().commit();
      System.out.println("after EntityManager.commit()");
      em.close();
  }


  public static void loadAndUpdateEntity() {
      System.out.println("-- loading and updating --");
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.println("before EntityManager.find()");
      MyEntity myEntity = em.find(MyEntity.class, 1);
      System.out.println("after EntityManager.find()");
      em.getTransaction().begin();
      System.out.println("before updating entity in transaction");
      myEntity.setMsg("new test msg");
      System.out.println("after updating entity in transaction");
      System.out.println("before EntityManager.commit()");
      em.getTransaction().commit();
      System.out.println("after EntityManager.commit()");
      em.close();
  }

  public static void mergeEntity() {
      System.out.println("-- merging --");
      MyEntity myEntity = new MyEntity();
      myEntity.setId(1);
      myEntity.setMsg("New merged msg");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      System.out.println("before EntityManager.merge()");
      em.merge(myEntity);
      System.out.println("after EntityManager.merge()");
      System.out.println("before EntityManager.commit()");
      em.getTransaction().commit();
      System.out.println("after EntityManager.commit()");
      em.close();
  }

  public static void loadAndRemoveEntity() {
      System.out.println("-- loading and removing --");
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      System.out.println("before EntityManager.find()");
      MyEntity myEntity = em.find(MyEntity.class, 1);
      System.out.println("after EntityManager.find()");
      System.out.println("before EntityManager.remove()");
      em.remove(myEntity);
      System.out.println("after EntityManager.remove()");
      System.out.println("before EntityManager.commit()");
      em.getTransaction().commit();
      System.out.println("after EntityManager.commit()");
      em.close();
  }
}
-- persisting --
before EntityManager.persist()
MyEntityListener.onPrePersist(): MyEntity{id=0, msg='test msg'}
after EntityManager.persist()
before EntityManager.commit()
MyEntityListener.onPostPersist(): MyEntity{id=1, msg='test msg'}
after EntityManager.commit()
-- loading and updating --
before EntityManager.find()
MyEntityListener.onPostLoad(): MyEntity{id=1, msg='test msg'}
after EntityManager.find()
before updating entity in transaction
after updating entity in transaction
before EntityManager.commit()
MyEntityListener.onPreUpdate(): MyEntity{id=1, msg='new test msg'}
MyEntityListener.onPostUpdate(): MyEntity{id=1, msg='new test msg'}
after EntityManager.commit()
-- merging --
before EntityManager.merge()
MyEntityListener.onPostLoad(): MyEntity{id=1, msg='new test msg'}
after EntityManager.merge()
before EntityManager.commit()
MyEntityListener.onPreUpdate(): MyEntity{id=1, msg='New merged msg'}
MyEntityListener.onPostUpdate(): MyEntity{id=1, msg='New merged msg'}
after EntityManager.commit()
-- loading and removing --
before EntityManager.find()
MyEntityListener.onPostLoad(): MyEntity{id=1, msg='New merged msg'}
after EntityManager.find()
before EntityManager.remove()
MyEntityListener.onPreRemove(): MyEntity{id=1, msg='New merged msg'}
after EntityManager.remove()
before EntityManager.commit()
MyEntityListener.onPostRemove(): MyEntity{id=1, msg='New merged msg'}
after EntityManager.commit()

Example Project

Dependencies and Technologies Used:

  • h2 1.4.196: H2 Database Engine.
  • hibernate-core 5.2.12.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.3.9

@EntityListeners Example Select All Download
  • jpa-entity-listener-class
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyEntity.java
          • resources
            • META-INF

    See Also