首页 > 解决方案 > @CreatedDate 不起作用,但 @UpdatedDate 正在为第一次保存工作

问题描述

我尝试使用@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"

标签: javamongodbspring-bootmongotemplateaudit-tables

解决方案


推荐阅读