Question

I'm trying to implement basic auditing using Spring Data JPA. From this question I learned that it is not yet possible to enable auditing using an annotation. So I have the following applicationContext.xml file in src/main/resources:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <jpa:auditing />
</beans>

I have added the @ImportResources("classpath:/applicationContext.xml") to my Java Config file.

On my AbstractEntity(which is a @MappedSuperClass) I have the following:

@MappedSuperclass
public abstract class AbstractEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Version
    private Integer version;
    @CreatedDate
    private Date createdDate;
    @LastModifiedDate
    private Date lastModifiedDate;

    // GETTERS AND SETTERS
}

Where java.util.Date has been imported. I have also tried with JodaTime but no change.

As far as I can tell this configuration should be sufficient to enable the auditing for dates. I have no need for the @CreatedByor @LastModifiedBy audits, so I don't think I need an AuditAware bean...even so, I have tried adding it but also without luck.

How do I get basic auditing to work?

Was it helpful?

Solution

First of all, Spring Data JPA 1.5 M1 ships with @EnableJpaAuditing so that you don't need the extra XML file if you're able to upgrade to this version.

It seems like you're missing the declaration of the AuditingEntityListener in the JPA config (usually the orm.xml config file). See the reference documentation for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top