我使用JPA和Spring的某些部分(如事务管理,JPA存储库),但我不使用Spring进行依赖注入,而是将Spring部分视为POJO对象。到目前为止,我的工作很好:我有运行时生成的JPA存储库类,事务由Spring类管理。以编程方式添加JPA EntityListener/Spring Data AuditingEntityListener以编程方式
但是,我似乎无法弄清楚如何让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
}
你可以看到我指定的春天AuditEntityListener
类。应用中的所有其他实体类都会扩展这个BaseEntity
类。
然后,我有一个实现AuditorAware
类:
public class JpaAuditConfiguration implements AuditorAware<Long>
{
@Override
public Long getCurrentAuditor()
{
//pretend there's real logic here...
return new Long(0);
}
}
现在,因为我没有使用Spring或Spring数据来引导自己,我需要一种方法来注册这个JpaAuditConfiguration
与AuditingEntityListener
。
我的问题:如何以编程方式注册JpaAuditConfiguration
和AuditEntityListener
?
如果有帮助,我使用Spring类如LocalContainerEntityManagerFactoryBean
(以编程方式创建EntityManagerFactory
)和PersistenceUnitPostProcessor
以编程方式执行JPA配置的其余部分。我正在寻找允许我做上面提到的实体审计监听器注册的钩子。
我没有使用任何orm.xml
或persistence.xml
JPA配置文件。
我该怎么做?
谢谢!
我没有使用Spring启动或Spring的DI,所以我不认为这些注释将任何区别。 – user3303372
那么你如何加载持久性上下文? – kuhajeyan