I have a Couchbase document for which I'd like to enable auditing:
@Document(expiry = 0, expiryUnit = TimeUnit.DAYS, touchOnRead = true)
public class Entity {
@Id
@GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES, delimiter = ":")
private String id;
@IdAttribute(order = 0)
private String type;
@IdAttribute(order = 1)
private String entityGuid;
@Version
private long version;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
@CreatedDate
private LocalDateTime createTimeStamp;
@LastModifiedDate
private LocalDateTime lastUpdateTimeStamp;
@CreatedBy
private String createdBy;
@LastModifiedBy
private String lastUpdatedBy;
...
My configuration:
@Data
@Configuration
@EnableCouchbaseAuditing
@EnableConfigurationProperties(CouchbaseProperties.class)
public class EntityCouchConfig extends AbstractCouchbaseConfiguration {
...
@Bean
public AuditorAware<String> couchAuditing() {
return () -> Optional.of("my-entity-service");
}
}
It was my expectation that when performing update operations through the Couchbase template like replaceById()
and upsertById()
, spring-data would preserve the document's @CreatedDate
and @CreatedBy
fields, only updating the @LastModifiedDate
and @LastModifiedBy
.
This, however, seems not to be the case. When I perform an update, the document's @Created
fields are updated as well. This seems counterintuitive, and would suggest that I first need to pull the document, transfer the @Created
fields, and then save that, explicitly making two calls.
I have read the spring-data-couchbase documentation on auditing but it's pretty sparse on the expected behavior here.
Is retrieving the document to capture the creation info and then updating the only way to do this, or am I implementing auditing wrong?