春季数据审计在我的项目中不起作用 [英] Spring data auditing not working in my project

查看:134
本文介绍了春季数据审计在我的项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图配置spring数据审计。

我使用Hibernate 4.1.6,hibernate-jpa 2.0,spring-data-jpa 1.1.0,spring 3.1.2(我已经添加了spring-aspects到dependecies,因为我得到了一个没有它的错误)。



我在我的项目配置上使用示例进行了一些修改:


  • 我的实体不扩展AbstractAuditable,但实现了Auditable接口
  • 我没有一个persistence.xml在哪里放置jadira属性和I在DateTime属性上使用了下列注释: @Type(type =org.jadira.usertype.dateandtime.joda.PersistentDateTime)



我没有任何错误,但审计不起作用:创建记录后审计字段为空。



有人知道我做错了什么吗?我可以检查什么?任何建议都是值得欢迎的。



以下是实体代码:

  @Entity 
public class TestEntity实现可审计< String,Long> {
@Column
@Id
@SequenceGenerator(...)
@GeneratedValue(...)
private Long id;

@Column
private String createdBy;

@Column
@Type(type =org.jadira.usertype.dateandtime.joda.PersistentDateTime)
private DateTime creationDate;

@Column
private String lastModifiedBy;

@Column
@Type(type =org.jadira.usertype.dateandtime.joda.PersistentDateTime)
private DateTime lastModifiedDate;

// getters and setters
}

这是spring上下文配置:

 < jpa:repositories base-package =..../> 

< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =.../>
< property name =mappingResources>
< value> ... / domain-orm.xml< / value>
< / property>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
...
< / bean>
< / property>
< ; property name =jpaProperties>
< util:properties>
< prop key =hibernate.generate_statistics> $ {hibernate.generate_statistics}< / prop>
< / util:properties>
< / property>
< / bean>
< context:annotation-config />

< bean我的审计员​​知道:
/ p>

  public class TestAuditorAware implements AuditorAware< String> {
@Override
public String getCurrentAuditor(){
returnTEST;
}
}

我的网域orm .xml:

 <?xml version = 1.0encoding =UTF-8?> 
< entity-mappings xmlns =http://java.sun.com/xml/ns/persistence/orm
xmlns:xsi =http://www.w3.org/2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd
version =2.0>
< persistence-unit-metadata>
< persistence-unit-defaults>
< entity-listeners>
< entity-listener class =org.springframework.data.jpa.domain.auditing.support.AuditingEntityListener/>
< / entity-listeners>
< / persistence-unit-defaults>
< / persistence-unit-metadata>
< / entity-mappings>

更新:我认为问题与AuditingEntityListener相关,没有注册。我添加了orm.xml配置,但没有解决。我认为听众仍然没有注册。

解决方案

我通过添加orm.xml文件解决了问题。起初它不工作,因为我还在文件名中添加了一个小错字(日志中没有显示错误)。


I'm trying to configure spring data auditing.

I use Hibernate 4.1.6, hibernate-jpa 2.0, spring-data-jpa 1.1.0, spring 3.1.2 (I've added spring-aspects to the dependecies since I got an error without it).

I've based my project configuration on this example with some modifications:

  • My entity don't extend AbstractAuditable but implements Auditable interface
  • I don't have a persistence.xml where to put the jadira properties and I used the following annotation on DateTime properties instead: @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")

I don't have any error but the auditing doesn't work: the auditing field are null after I create a record.

Does anyone know what I've done wrong? What can I check? Any suggestion is welcome.

Here is the entity code:

@Entity
public class TestEntity implements Auditable<String, Long> {
  @Column
  @Id
  @SequenceGenerator(...)
  @GeneratedValue(...)
  private Long id;

  @Column
  private String createdBy;

  @Column
  @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
  private DateTime creationDate;

  @Column
  private String lastModifiedBy;

  @Column
  @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
  private DateTime lastModifiedDate;

  // getters and setters
}

This is the spring context configuration:

<jpa:repositories base-package="...." />

<jpa:auditing auditor-aware-ref="auditorAware" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="..." />
  <property name="mappingResources">
    <value>.../domain-orm.xml"</value>
  </property>
  <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    ...
    </bean>
  </property>
  <property name="jpaProperties">
    <util:properties>
      <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
    </util:properties>
  </property>
</bean>
<context:annotation-config />

<bean id="auditorAware" class="...TestAuditorAware"/>

My auditor aware:

public class TestAuditorAware implements AuditorAware<String> {
  @Override
  public String getCurrentAuditor() {
    return "TEST";
  }
}

My domain-orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="2.0">
  <persistence-unit-metadata>
    <persistence-unit-defaults>
      <entity-listeners>
        <entity-listener class="org.springframework.data.jpa.domain.auditing.support.AuditingEntityListener" />
        </entity-listeners>
      </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

Update: I think that the problem is related to AuditingEntityListener that doesn't get registered. I added a the orm.xml configuration but this didn't solved. I think the listener still doesn't get registered.

解决方案

I solved the problem by adding the orm.xml file. At first it didn't work because I also added a small typo in the file name (no error displayed in the log).

这篇关于春季数据审计在我的项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆