spring-projects / spring-projects/spring-data-rest
Mongo Auditing: @CreatedDate field gets set to null on updates with Spring Data Rest [DATAREST-1204]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Benjamin Cody opened DATAREST-1204 and commented
I've set up a Spring Boot application with spring-boot-starter-data-rest and spring-boot-starter-data-mongodb. I'm using the Mongo auditing feature to automatically set insert and update timestamps on my records using the @CreatedDate and @LastModifiedDate annotations.
I'm using the generated REST/HAL endpoint to insert and update records. When I insert a record, its insert and update timestamp both get set to the current time, as expected. However, when I perform an update, the insert timestamp get overwritten with null! :( The update timestamp gets updated correctly (as does the @Version annotated field).
This issue occurs when I have public getters/setters for my insert/update fields. When I remove those getters/setters, the fields get written as I would expect: the insert timestamp only gets written once on inserts and doesn't change on updates, and the update timestamp always gets written. With the @Version annotated field, it doesn't matter whether or not I have a public getter/setter: the version is never output by the REST endpoint and also cannot be set explicitly by the client.
I prepared a minimal project with a test which shows this scenario: https://github.com/bencody/spring_data_mongo_createddate
Affects: 2.6.10 (Ingalls SR10)
Reference URL: https://github.com/bencody/spring_data_mongo_createddate
2 votes, 4 watchers
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.