问题
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.
回答1:
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 should also use the @Document annotation instead of implementing the Persistable Class, like this:
@Document
public class Profile{}
回答2:
You just ought to add @Version filed to you @Document class and left @EnableMongoAuditing enabled. It will look something like this:
@Document
public class Profile implements Persistable<String>{
@Vesrion
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
回答3:
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.
回答4:
For me, i just do it as:
@CreatedDate
@Field("created_date")
@JsonIgnore
private Instant createdDate = Instant.now();
And make sure the Get/Set is available. Hope this help
来源:https://stackoverflow.com/questions/47054719/spring-data-mongodb-annotation-createddate-does-not-work-while-using-with-cus