Skip to content

Spring Data Couchbase @CreatedDate not working

I’m using Spring boot with spring data couchbase. I’ve added two fields for createdDate and lastModifiedDate with @CreatedDate and @LastModifiedDate annotations in Movie Document.

lastModifiedDate works great but createdDate is always null.

@RequiredArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode
@Document
public class Movie {

    @Id
    @NonNull
    @Getter
    private String id;

    @Getter
    @Setter
    @NonNull
    private String title;

    @Getter
    @LastModifiedDate
    private Date lastModifiedDate;

    @Getter
    @CreatedDate
    private Date createdDate;
}

I’ve also added a configuration for @EnableCouchbaseAuditing:

@Configuration
@EnableCouchbaseAuditing
public class AuditConfiguration {
}

Movie Repository:

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "movie")
public interface MovieRepository extends CouchbaseRepository<Movie, String> {

    Collection<Movie> findByTitle(String title);

    Collection<Movie> findByTitleLike(String title);

    Collection<Movie> findByTitleStartingWith(String title);

}

application.yml for reference:

spring:
  couchbase:
    bootstrap-hosts: localhost
    bucket:
      name: movie
  data:
    couchbase:
      auto-index: true

Answer

As stated in the documentation, in order to distinguish between a creation and an update, spring-data-couchbase needs a @Version annotated attribute in the entity class