Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...