我尝试使用@CreatedDate和@LastModifiedDate。虽然@LastModifiedDate可以正常工作,但@CreatedDate却不能。
我在主应用程序配置文件中添加了@EnableMongoAuditing。
mainapplication.java
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(Application.class, args);
}
}
Comment.java
@Entity
public class Comment {
@Id
String commentId;
@NotNull
String userId;
@CreatedDate
Date CreatedDate;
@LastModifiedDate
Date UpdatedDate;
getters, setters .,,
}
CommentDAO.java
public class CommentDAO{
@Autowired
MongoTemplate mongoTemplate;
public Comment addComment(Comment comment) {
return mongoTemplate.save(comment, "Comment");
}
}
controller.java
@annotations
class controller
{
@GetMapping("/hello")
public Comment check()
{
Comment c=new Comment();
c.setUserId("kli");
c.setAnonymus(false);
return commentDAO.addComment(c);
}
}
结果是
commentId "0.07864261262777905"
userId "kli"
content null
anonymus false
createdDate null
updatedDate "2019-01-19T08:36:42.573+0000"