Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually

前端 未结 1 682
既然无缘
既然无缘 2020-12-29 11:59

I\'m trying to use auditing to save dateCreated and dateUpdated in my objects, but since I set ID manually, there\'s some additional w

相关标签:
1条回答
  • 2020-12-29 12:27

    Your code is working as expected. After you've implemented Persistable you can see that @CreatedDate annotation is working.

    Sure that createdDate is null on the second call of save because the object already exists in the database and you updated it with createdDate = null. As you can see from the documentation for @CreatedDate:

    @CreatedDate annotation. This identifies the field whose value is set when the entity is persisted to the database for the first time.

    So not to overwrite your createdDate with null on the second call you should retrieve your customer from the database with c = repository.findOne("test_id"); and then update it.

    0 讨论(0)
提交回复
热议问题