Miguel Guilherme
Miguel Guilherme

Reputation: 105

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

Upvotes: 1

Views: 1738

Answers (3)

Abdur Rahman
Abdur Rahman

Reputation: 1656

In my case, when I updated my application with spring boot and spring-boot-couchbase, I also face a similar issue. @CreatedDate and @LastModifiedDate annotations can't update the date time itself in real-time. Finally, it was solved when I added @EnableCouchbaseAuditing in the Application.

here is the pom update

 <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-couchbase</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>3.0.5</version>
</dependency>

And the Application class

@SpringBootApplication
@EnableCouchbaseAuditing
public class Application {

     public static void main(String[] args) {
         SpringApplication.run(Application.class, args);
    }
}

Upvotes: 0

antonkronaj
antonkronaj

Reputation: 1367

If anyone lands here ... I had an issue with the createdDate not being populated even though I followed the spring-data-couchbase documentation. I followed the process of creating immutable objects for my Document and Auditing. The cause was the field annotated with @CreatedDate was final (I used lombok @Value). I had to make it non-final (@NonFinal using Lombok) for it to finally work.

Upvotes: 0

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28301

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

Upvotes: 1

Related Questions