spring-projects / spring-projects/spring-data-mongodb

Allow Date as a @Version field [DATAMONGO-2102]

Open
#2,970 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 30, 2020.

in: core type: documentation type: enhancement
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Iryna Tserlizhenka opened DATAMONGO-2102 and commented

It would be great to have ability to use java.util.Date as a version field for optimistic locking:

 

public class DateVersioned {
    @Id
    private String id;
    @Version
    private Date dateVersion;
    ...
}

 

So far I found a way to make it work with existing objects through converters (see reference URL for details), but not with new ones. When saying existing objects, I mean documents that already had dateVersion field in the DB before @Version was applied.

When using converters for new objects the date version is stored in the DB as NumberLong, but queried as a date in optimistic lock clause. Therefore optimistic lock always fails.

Expected behavior: adding @Version annotation on the Date field should be enough to make it work. Possibly we'll need some additional configuration like @Temporal in JPA. The date fields should be stored in the DB as dates, not as numbers.

It would also be nice to update the documentation on types that can be used as a version. The current documentation does not include this information but mentions similarity to JPA where we can use Date mapped to timestamp for some providers


Affects: 2.0.10 (Kay SR10)

Reference URL: https://stackoverflow.com/questions/52604081/spring-optimistic-lock-for-mongodb-document-with-java-util-date-field

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.