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.
Auditableinterface and set the audit fields yourself in case of custom id. More hereAuditablemay not work because isNew will return false and spring repository will copy only the lastModifed field from entity. So it looks like you have to update the isNew implementation to differentiate between insert and update request.