Spring Data MongoDB - Annotation @CreatedDate does not work while using with custom Id field

12,095

Solution 1

simply just add @Version field to you @Document class and leave @EnableMongoAuditing i.e.

@Document
public class Profile implements Persistable<String>{

     @Version      
     private Long version;
    
     @Id
     private String username;

     @CreatedDate
     public Date createdDate;

     public Profile(String username) {
         this.username = username;
     }

     @Override
     public String getId() {
         return username;
     }

     @Override
     public boolean isNew() {
         return username == null;
     }
 }

Here is a related issue: https://jira.spring.io/browse/DATAMONGO-946

Solution 2

I just ran into this problem myself, it happens because you are creating the id yourself.

public Profile(String username) {
        this.username = username;
    }

By doing this, mongo thinks it is not a new object and doesn't use the @CreatedDate annotation. You could also use the @Document annotation instead of implementing the Persistable Class, like this:

@Document
public class Profile{}

Solution 3

As described in Spring Data MongoDB issue DATAMONGO-946, the created date functionality uses the isNew() method to determine whether the created date should be set since the entity is new. In your case, your isNew method is always returning false, since the username is always set.

The comments in the issue presents two possible solutions to this problem.

Persistable solution

The first option is to fix the isNew strategy so that it correctly registers new objects. One way suggested in the comments would be to change the implementation to check the createdDate field itself, since it should only be set on non-new objects.

@Override
public boolean isNew() {
    return createdDate == null;
}

Persistent entity solution

The second option is to change from implementing Persistable to instead use a persistent entity and use the @Version annotation to inject a version property in the persisted MongoDB entity. Note that this will change how the data is persisted, as it adds an auto-incrementing version field to the data.

import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Profile {
    @Id
    private String username;

    @CreatedDate
    public Date createdDate;

    @Version
    public Integer version;

    public Profile(String username) {
        this.username = username;
    }
}

Solution 4

If that's your real class (Profile) then there's nothing you can do with standard tools.

Your isNew method will always return false, because you set the username by yourself and when Profile is about to be saved by Spring it will check the isNew and you probably have username already set. @LastModifiedDate would work in your case, but @CreatedDate won't.

If you haven't got other fields that you can use in isNew method then you have to set the value of createdDate manually (or maybe there's some kind of interceptor that may wrap all mongo template method, but I wouldn't go that way).

For example, check if profile with given username already exists in DB, if so just get it's createdDate(you can use projection here) and set to the profile that you're about to save. Otherwise set createdDate to new date.

Share:
12,095
akcasoy
Author by

akcasoy

Updated on June 12, 2022

Comments

  • akcasoy
    akcasoy almost 2 years

    I have a simple Persistable Class:

    public class Profile implements Persistable<String>{
    
        @Id
        private String username;
    
        @CreatedDate
        public Date createdDate;
    
        public Profile(String username) {
            this.username = username;
        }
    
        @Override
        public String getId() {
            return username;
        }
    
        @Override
        public boolean isNew() {
            return username == null;
        }
    }
    

    And a simple repository:

    public interface ProfileRepository extends MongoRepository<Profile, String> {
    
    }
    

    My Spring Boot Application class is also annotated with @EnableMongoAuditing. But i still can't get the annotation @CreatedDate work.

    ProfileRepository.save(new Profile("user1")) writes the entity without the field createdDate. What do i do wrong?

    EDIT: This is my Application class (without @EnableMongoRepositories, but it works since the repositories are in the sub-packages i guess)

    @SpringBootApplication
    @EnableMongoAuditing
    public class Application {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    }
    

    EDIT: Also adding the annotation EnableMongoRepositories did not change anything.