@Version with @Lastmodifieddate

People. I was thinking about the easiest way to avoid the lost update in spring. Inside a @Transactional method I am updating persistent object. To avoid the lost update possibility @LastModifiedDate and @Version annotation should deal with it with something like:

  @LastModifiedDate
  @Version
  private DateTime updateTime;

I think that is a short way to achieve what I need, but do you know any downsides of these? Shall they work as expected? Every piece of info will be greatly appreciated. Best Regards.

4

1 Answer

I don't think there are any downsides - it works as expected for me with Spring 5.1.5 and Spring Data JPA 2.1.5. I didn't find anyone complaining neither.

Edit (10/07/2019)

The only issue I found is when you use field of Date type as your timestamp and it is stored as DATETIME in MySQL v5.6 (it has been fixed in MySQL v5.7). DATETIME precision is down to seconds, so if you have two transactions running in a matter of the same second, version won't be updated and Entity got to the state I couldn't update it anymore.

TLDR - If you're planning to use @Version together with @LastModifiedDate, make sure that timestamp is stored with precision to at least milliseconds and it may have limitations if you ever access it even more often

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest