我正在尝试在 JPA 实体上实现默认监听器。我读过几篇文章,所有文章都提到了:
Currently, default listeners can only be specified in a mapping XML file because there is no equivalent annotation
我们是否有任何解决方法来实现默认监听器而不使用 XML 文件。
using: Spring Boot, Spring Data JPA - (Java Configuration)
最佳答案
您可以通过这种方式将您的监听器添加到您的实体主体:
@Entity
public class MyEntity{
//attributes & getters and setters
@PrePersist void onPrePersist() {}
@PostPersist void onPostPersist() {}
@PostLoad void onPostLoad() {}
@PreUpdate void onPreUpdate() {}
@PostUpdate void onPostUpdate() {}
@PreRemove void onPreRemove() {}
@PostRemove void onPostRemove() {}
}
或以这种方式使用@EntityListeners
注解:
@Entity
@EntityListeners({MyListener1.class, MyListener2.class})
public class MyEntity {
}
和:
公共(public)类 MyListener1 {
@PrePersist void onPrePersist(Object o) {}
@PostPersist void onPostPersist(Object o) {}
@PostLoad void onPostLoad(Object o) {}
@PreUpdate void onPreUpdate(Object o) {}
@PostUpdate void onPostUpdate(Object o) {}
@PreRemove void onPreRemove(Object o) {}
@PostRemove void onPostRemove(Object o) {}
}
关于java - 在没有 XML 配置的情况下实现 JPA 默认监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451016/