Jay Jay - 2 months ago 26
Java Question

How to configure Auditing via Java Config in Spring Data (and Spring Data Rest)?

I am trying to use Spring Data's auditing capabilities (in combination with Spring Boot and Spring Data Rest), but the audit fields are not being set on save. All saves result in a constraint exception from trying to save a null "Created By."

According to the spring data docs, I should just be able to place the appropriate auditing annotations (@CreatedDate/etc) on my Entity, and make an AuditorAware<> available to the application context. I know my auditor aware bean is being created from setting a breakpoint in the debugger.

My questions are:

1) Is it necessary for me to create an AuditingEntityListener, or should I expect one to be provided from having @EnableJpaAuditing? (it's not clear in the docs about java config)

2) Is there other configuration in the below code that I'm missing to set up automatic auditing?

3) I'm calling the creation code from a POST to Spring Data Rest, are there any special caveats with using this auditing functionality in combination with Spring Data Rest?

@Entity
public class Tag implements Serializable {

// ... other fields omitted...

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date created = new Date();

@CreatedBy
@Basic(optional = false)
@Column(name = "CREATED_BY", nullable = false, length = 24)
private String createdBy = "";

@LastModifiedDate
@Basic(optional = false)
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date updated = new Date();

@LastModifiedBy
@Basic(optional = false)
@Column(name = "UPDATED_BY", nullable = false, length = 24)
private String updatedBy = "";

// ... getters and setters were generated ...


And the configuration:

@EnableJpaAuditing
@Configuration
public class AuditingConfig {

@Bean
public AuditorAware<String> createAuditorProvider() {
return new SecurityAuditor();
}

@Bean
public AuditingEntityListener createAuditingListener() {
return new AuditingEntityListener();
}

public static class SecurityAuditor implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
return username;
}
}

}


Any help is much appreciated, thanks!

Answer

1) Is it necessary for me to create an AuditingEntityListener, or should I expect one to be provided from having @EnableJpaAuditing? (it's not clear in the docs about java config)

Answer: No you do not need to define AuditingEntityListener bean. Instead you need to specify @EntityListeners(AuditingEntityListener.class) on your domain class.

e.g.

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Tag implements Serializable {

}

2) Is there other configuration in the below code that I'm missing to set up automatic auditing?

Answer: Other configuration settings look fine.

3) I'm calling the creation code from a POST to Spring Data Rest, are there any special caveats with using this auditing functionality in combination with Spring Data Rest?

Answer: I think not. Try with above suggested change. It should just work.