Spring data auditing not working in my project

2019-08-15 22:17发布

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.

1条回答
三岁会撩人
2楼-- · 2019-08-15 22:52

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).

查看更多
登录 后发表回答