java实体类绑定监听_java – 以编程方式添加JPA EntityListener / Spring Data AuditingEntityListener...

我正在使用JPA和Spring的一部分(如事务管理,JPA存储库),但我不使用Spring进行依赖注入,而是将Spring部分视为POJO对象.到目前为止,我的工作非常好:我有运行时生成的JPA存储库类,并且事务由Spring类管理.

但是,我似乎无法弄清楚如何让JPA审计监听器工作.

例如,这是我的BaseEntity,它定义了EntityListener类和审计字段:

@MappedSuperclass

@EntityListeners( { AuditingEntityListener.class } )

public class BaseEntity implements Serializable

{

@CreatedDate

@Field( index = Index.YES, store = Store.YES )

@Column( name = "date_created" )

@Temporal( TemporalType.TIMESTAMP )

private Date dateCreated;

@CreatedBy

@Column( name = "created_by" )

private Long createdBy;

//other stuff

}

您可以看到我指定了Spring AuditEntityListener类.应用程序中的所有其他实体类都扩展了此BaseEntity类.

然后,我有一个实现AuditorAware的类:

public class JpaAuditConfiguration implements AuditorAware

{

@Override

public Long getCurrentAuditor()

{

//pretend there's real logic here...

return new Long(0);

}

}

现在,因为我没有使用Spring或Spring Data来自行启动,所以我需要一种方法来使用AuditingEntityListener注册这个JpaAuditConfiguration.

我的问题:如何以编程方式向AuditEntityListener注册JpaAuditConfiguration?

如果它有帮助,我使用Spring类,如LocalContainerEntityManagerFactoryBean(以编程方式创建EntityManagerFactory)和PersistenceUnitPostProcessor以编程方式执行其余的JPA配置.我正在寻找允许我做上面提到的实体审计监听器注册的钩子.

我没有使用任何orm.xml或persistence.xml JPA配置文件.

我怎样才能做到这一点?

谢谢!!

解决方法:

@Configuration

@EnableJpaAuditing

class Config {

@Bean

public AuditorAware auditorProvider() {

return new JpaAuditConfiguration ();

}

}

标签:java,spring,jpa,spring-data,spring-data-jpa

来源: https://codeday.me/bug/20190706/1393289.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值