Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

java - 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?

question from:https://stackoverflow.com/questions/10648515/using-version-in-spring-data-project

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

1 Reply

0 votes
by (71.8m points)

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" row 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!

Thanks all


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...