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!
Enable JpaAudit In order to enable JPA Auditing for this project will need to apply three annotations and a configuration class. Those annotations are; @EntityListener , @CreatedDate , and @LastModifiedDate . @EntityListener will be the one that is responsible to listen to any create or update activity.
Auditing basically involves tracking and logging every change we make to our persisted data, which consists of tracking and storing every insert, update, and delete activity. Auditing aids in the preservation of history records, which can later be used to trace user activity.
Annotation Type Audited. When applied to a class, indicates that all of its properties should be audited. When applied to a field, indicates that this field should be audited.
Spring Boot JPA Audit Logging Example Spring Data helps you keep track of who created or modified an entity, as well as when it happened. To leverage this auditing functionality, you must provide auditing metadata to your entity classes, which can be defined using annotations or by implementing an interface.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With