我正在使用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