Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 952
Matt Whipple Avatar asked Mar 22 '23 03:03

Matt Whipple


1 Answers

I know this is an old question, but I hit the same problem and a comment helped me resolve it. So I thought I would make it clearer if anyone falls on this question again.

The documentation for Spring Data is a bit misleading in that it implies that you can enable auditing simply by annotating a @Configuration class with @EnableJpaAuditing.

However, the part I found unclear is that you still need to modify the orm.xml file (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#auditing):

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

However, if you are using a pure annotation based solution, you may not have an orm.xml file. As indicated by Matt Whipple in a comment, you have to add the @EntityListeners annotation to you entity classes so that the JPA persistence library calks the Spring auditing class when persisting the objects (which in turn deals with the auditing).

So a complete example could be something like:

@Configuration
@EnableJpaAuditing
@PropertySource({ "application.properties" })
public class AppConfig {
  /**
     * Stubbed method for the auditor as the app does not concern itself with auditing any User fields
     * Consequently, return null for the current auditor
     * @return
     */
    @Bean
    public AuditorAware<User> auditorProvider(){
        return new AuditorAware<User>() {
            @Override
            public User getCurrentAuditor() {
                return <User object that is Logged In>;
            }
        };
    }
}

Then on your entity:

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Log {

    @Id
    @GeneratedValue
    private long id;

    @CreatedDate
    @Column(nullable=false)
    private Date createdOn;

    // bunch of other audit fields (and other fields)
    ...
    ...
}
like image 64
Eric B. Avatar answered Apr 02 '23 12:04

Eric B.