2016-11-14 54 views
3

我使用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数据来引导自己,我需要一种方法来注册这个JpaAuditConfigurationAuditingEntityListener

我的问题:如何以编程方式注册JpaAuditConfigurationAuditEntityListener

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

我没有使用任何orm.xmlpersistence.xml JPA配置文件。

我该怎么做?

谢谢!

回答