Hibernate Entity Listener for named query delete operation

2.5k views Asked by At

For our application we need to implement trigger using hibernate. One best solution we could come to was Entity listerner using Annotation, as we need to listen to a particular entity change.

Every thing works well except delete with named queries, which gives no event.

Code Implementation ** Entity** -here we added the listener

@Entity
@EventListeners(EmployeeEventListener.class)
public class Employee {

  @Id
  private String uid;
  @Basic
  private Calendar lastUpdated;

Entity listener -

Listener is takes up the entity modified asd performs the intended operation

public class EmployeeEventListener {
  @PrePersist
  public void prePersist(Object object) {
    Employee employee = (Employee)object;
    employee.setUID(UIDGenerator.newUUI());
    employee.setLastUpdated(Calendar.getInstance());
  }
  @PostUpdate
  public void postUpdate(Object object) {
    Employee employee = (Employee)object;
    employee.setLastUpdated(Calendar.getInstance());
  }

@PrePersist and @PostUpdate worked well when I used save or saveorupdate on entity manager. But when executing delete named query, I get no event for @PreRemove and @PostRemove

I would also like to get an event for delete as well.

1

There are 1 answers

6
Luca Basso Ricci On BEST ANSWER

This is not possible: The Interceptor interface provides callbacks from the session to the application and object deleted via native SQL doesn't pass to session, so callback won't run