I've been working on a RESTful webservice with spring-data. A few days ago a special spring-data jpa REST framework was released.
Now I noticed the ability to use @Version with this framework. Is this version generated by itself or do you need to do this manually?
And is it possible to use @Version on it's own? (So that I don't have to change anything to my existing repositories/domain etc..)
And do I need to do some extra configuration to make use of @Version?
@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...
Version used to ensure that only one update in a time. JPA provider will check the version, if the expected version already increase then someone else already update the entity so an exception will be thrown.
Spring data jpa- it is same like jpa means we can describe in below way. Spring Data Jpa is jpa data abstraction access which means it likes a jpa but it add some extra functionality, Without jpa we can not implement the spring data jpa.
Spring Data is a wrapper around an ORM framework.
It's been a while since I posted this question but I've figured it out. I'll explain what I did so that it might be helpful to someone else.
The annotation @Version
is a javax.persistence interface and not the spring-data rest jpa framework as i mentioned earlier.
If you want to make use of @Version you need to create an version field in your domain object like so:
@Version @Column(name = "VERSION") private long version;
If you're using hibernate it will automatically pickup the annotation and it will create a "version" column in your (in my case MySql) table. Every time a record gets updated, hibernate will increment the counter with 1.
Now why is this something you want? Well the reason why you might wanna use this is because it decreases the chance that your clients are working with stale data. Whenever a client retrieves information from you a version is provided with the data he requested. e.g.
{ <-- School entity --> "id": 1, "version": 0, "name": "De regenboog", "street": "Plantaanstraat", "number": "2", "zipCode": "1234AS", "city": "Amsterdam" }
Now if a client wants to change some information about this specific record it sends the new information along with the version value. In this case let's change the name of the school.
{ <-- School entity --> "id": 1, "version": 0, "name": "Stackoverflow", "street": "Plantaanstraat", "number": "2", "zipCode": "1234AS", "city": "Amsterdam" }
Hibernate comes up with a query with your information and adds an extra 'where' clause to check the version. update .... where id = 1 and version = 0
. Now if the row is updated it means you provided the right version and no one else has changed that specific information between the time you requested the information, changed it and sent it back. Nice right?
Now what if the row isn't updated? It means someone else updated that row while you were taking a quick bathroom break after you requested the information. It means your version is outdated! What needs to happen now is really use case specific so I won't go into details about that
Hope someone can use this piece of information!
Make sure to import import javax.persistence.Version
and not the spring one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With