4

I have tried to use @CreatedDate and @LastModifiedDate. While @LastModifiedDate works fine but @CreatedDate is not.

I have added @EnableMongoAuditing in main application configuration file.

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.setId("0.07864261262777905");
        c.setUserId("kli");
        c.setAnonymus(false);
        return commentDAO.addComment(c);
    }
 }

result is

commentId   "0.07864261262777905"
userId  "kli"
content null
anonymus    false
createdDate null
updatedDate "2019-01-19T08:36:42.573+0000"
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Raghu
  • 43
  • 6
  • Does https://stackoverflow.com/questions/20483841/spring-data-createddate-annotation-doesnt-work-for-me help ? ... in other words: there are plenty of other questions here for that annotation. Have you studied them before putting up your question? (hint: you should) – GhostCat Jan 19 '19 at 08:49
  • @GhostCat I have tried, Actually i didnot using a seperate audit class, Also CreatedDate alone is not working for me – Raghu Jan 19 '19 at 08:58
  • commentId is auto generated or you are assigned manually – IMParasharG Jan 19 '19 at 10:09
  • @GovindParashar I have assigned manually. – Raghu Jan 19 '19 at 10:10
  • @Raghu : Then it wont work. readout https://jira.spring.io/browse/DATAMONGO-946 – IMParasharG Jan 19 '19 at 10:12
  • Thank you @GovindParashar . It works, Kindly let me know is there any restrictions for LastModifiedDate . Because while updating it is not works. – Raghu Jan 20 '19 at 01:32
  • @Raghu : I have to go through https://docs.spring.io/spring-data/mongodb/docs/1.10.7.RELEASE/reference/html/#auditing – IMParasharG Jan 21 '19 at 10:00
  • @GovindParashar This is the same issue that i am facing https://stackoverflow.com/questions/39296277/mongotemplate-updatefirst-doesnt-update-lastmodifieddate?noredirect=1&lq=1 . Kindly share answer if you know. – Raghu Jan 22 '19 at 04:43

0 Answers0