Raghu
Raghu

Reputation: 43

@CreatedDate is not working, but @UpdatedDate is working for the first save

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"

Upvotes: 4

Views: 1695

Answers (0)

Related Questions