javaspring-data-jpa

Spring Data JPA Auditing never getting called at runtime


I'm presently struggling with getting Spring Data JPA Auditing to work, it is presently not setting the fields and doesn't seem to be getting called in any way when working with Entities. In particular any insight into how it hooks into the standard flow of persisting Entities would be helpful.

I'm presently using Spring Data JPA 1.5.0.M1 with Spring 3.2.6 and the basic configuration for the auditing piece is:

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableJpaRepositories(basePackages = "org.myproject.dao")
@EnableTransactionManagement
public class JpaConfig {
...}

the relevant entity at the moment is marked up with the annotations and the interface while trying to work this out (the annotations would be preferred). I realize this should not be done but I copied and pasted for the moment.

@Entity
public class AutoDraft implements Auditable<Long, Long> {

    @SequenceGenerator(name="seq_auto_draft", sequenceName="SEQ_AUTO_DRAFT")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_auto_draft")
    @Id
    private Long id;

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

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

In the logs the relevant beans are being set up and I can catch the AuditingHandler being properly configured within the AuditingEntityListener on startup, but nothing seems to be getting triggered at runtime, nor are there any audit related logging messages associated with specific entities or repositories. My attention is presently drawn by the AuditingBeanFactoryPostProcessor, but I've already spent too long on this so could use any assistance.


Solution

  • It seems like you forgot to configure AuditingEntityListener in the orm.xml (which is still neccessary). Add this lines to your orm.xml:

    <persistence-unit-metadata>
      <persistence-unit-defaults>
        <entity-listeners>
          <entity-listener class="….data.jpa.domain.support.AuditingEntityListener" />
        </entity-listeners>
      </persistence-unit-defaults>
    </persistence-unit-metadata>
    

    see: Spring data jpa documentation: Auditing.