java - 在没有 XML 配置的情况下实现 JPA 默认监听器

标签 java spring jpa spring-boot jpa-2.1

我正在尝试在 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/

相关文章:

java - 调用 Camera.release() 后相机正在被使用

java - 如果我们在主线程中创建 Java Swing 应用程序会发生什么?

java - Hibernate/JPA DB 模式生成最佳实践

java - Hibernate 可以在性能敏感的应用程序中使用吗?

java - Eclipse + 将现有项目转变为 JP​​A 项目

java - 在 OnPostExecute() 中使用 StartActivity

java - 开发Eclipse插件时出现ClassNotFoundException

java - Spring MVC 4.1.x RestFul 服务抛出 Json Not Acceptable

javascript - spring JavaScript Promises 回调函数参数

java - Logback - 如何单独记录异常的简单名称