我尝试使用@CreatedDate 和@LastModifiedDate。虽然 @LastModifiedDate 工作正常,但 @CreatedDate 不是。
我在主应用程序配置文件中添加了@EnableMongoAuditing。
主应用程序.java
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(Application.class, args);
}
}
评论.java
@Entity
public class Comment {
@Id
String commentId;
@NotNull
String userId;
@CreatedDate
Date CreatedDate;
@LastModifiedDate
Date UpdatedDate;
getters, setters .,,
}
评论DAO.java
public class CommentDAO{
@Autowired
MongoTemplate mongoTemplate;
public Comment addComment(Comment comment) {
return mongoTemplate.save(comment, "Comment");
}
}
控制器.java
@annotations
class controller
{
@GetMapping("/hello")
public Comment check()
{
Comment c=new Comment();
c.setId("0.07864261262777905");
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"