I have a Spring Boot 1.3.M1 web application using Spring Data JPA. For optimistic locking, I am doing the following:
- Annotate the version column in the entity:
@Version private long version;
. I confirmed, by looking at the database table, that this field is incrementing properly. - When a user requests an entity for editing, sending the
version
field as well. - When the user presses submit after editing, receiving the
version
field as a hidden field or something. Server side, fetching a fresh copy of the entity, and then updating the desired fields, along with the
version
field. Like this:User user = userRepository.findOne(id); user.setName(updatedUser.getName()); user.setVersion(updatedUser.getVersion()); userRepository.save(user);
I was expecting this to throw exception when the versions wouldn't match. But it doesn't. Googling, I found some posts saying that we can't set the @Vesion
property of an attached entity, like I'm doing in the third statement above.
So, I am guessing that I'll have to manually check for the version mismatch and throw the exception myself. Would that be the correct way, or I am missing something?