Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Version in spring-data project

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?

like image 662
Byron Voorbach Avatar asked May 18 '12 07:05

Byron Voorbach


People also ask

What is the use of @data in spring boot?

@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, ...

What is @version in JPA?

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.

What is the difference between JPA and Spring data JPA?

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.

Is Spring data an ORM?

Spring Data is a wrapper around an ORM framework.


2 Answers

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!

like image 54
Byron Voorbach Avatar answered Sep 23 '22 09:09

Byron Voorbach


Make sure to import import javax.persistence.Version

and not the spring one.

like image 21
Byorn Avatar answered Sep 23 '22 09:09

Byorn