I am trying to use Spring's data audit capabilities (in combination with Spring Boot and Spring Data Rest), but audit fields are not set when saved. All saved results result in exclusion of the restriction from the attempt to keep the zero βCreatedβ.
According to spring data docs , I just have to put the appropriate audit annotations (@ CreatedDate / etc) on my Entity and make AuditorAware <> available to the application context. I know that my auditor knows that a bean is created from setting a breakpoint in the debugger.
My questions:
1) Do I need to create an AuditingEntityListener, or should I expect it to be provided from @EnableJpaAuditing? (this is not clear in the java config docs)
2) Is there any other configuration in the code below that I donβt have enough to set up automatic auditing?
3) I call the creation code from POST to Spring Data Rest, are there any special caveats using this audit function in conjunction 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!
source share