Populating the auditing information is done by the RelationalAuditingCallback
and IsNewAwareAuditingHandler
.
The first one basically is the adapter to the module specific part (Spring Data Relational in this case) while the second modifies the entity.
You can implement your own variant of the IsNewAwareAuditingHandler
stuff it in a RelationalAuditingCallback
and register it as a bean. I did something similar a short time ago in this project on GitHub:
@Bean
RelationalAuditingCallback isNewAwareAuditingHandler(JdbcMappingContext context) {
return new RelationalAuditingCallback(new CustomAuditingHandler(context));
}
private static class CustomAuditingHandler extends IsNewAwareAuditingHandler {
public CustomAuditingHandler(JdbcMappingContext context) {
super(PersistentEntities.of(context));
}
@Override
public Object markAudited(Object source) {
if (!(source instanceof Product)) {
return source;
}
Product product = (Product) source;
if (product.createdDate == null) {
product.createdDate = Instant.now();
}
return source;
}
}
Please consider the logic in the CustomAuditingHandler
a place holder. There you should plugin your way to determine if you set the value manually. Maybe your entity implements an interface that offers that information as a transient field, or you store that information in a thread local variable.