在spring-data项目中使用@Version [英] Using @Version in spring-data project

查看:112
本文介绍了在spring-data项目中使用@Version的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用spring-data开发一个RESTful webservice。几天前,一个特殊的spring-data jpa REST框架发布了



现在我注意到在这个框架中使用@Version的能力。这个版本是由它自己生成还是你需要手动完成?



它可以自己使用@Version吗? (所以我不必改变任何东西到我现有的仓库/域等)。

我是否需要做一些额外的配置来使用@版本?

解决方案

我发布了这个问题已经有一段时间了,但我已经知道了。我会解释我做了什么,以便它可能对别人有帮助。



注释 @Version 是一个javax.persistence接口,而不是前面提到的spring-data rest jpa框架。



如果您想使用@Version,您需要在域对象中创建一个版本字段,如下所示:

  @Version 
@Column(name =VERSION)
私人长版;

如果您使用休眠模式,它会自动拾取注释,并创建一个版本行在你的(在我的情况下MySql)表。每次记录更新时,hibernate都会用1递增计数器。



现在为什么这是你想要的东西?那么你可能想要使用它的原因是因为它降低了你的客户使用陈旧数据。每当客户从您那里获取信息时,都会提供他所请求的数据。例如

  {<  - 学校实体 - > 
id:1,
version:0,
name:de regenboog,
street:Plantaanstraat,
数字:2,
zipCode:1234AS,
city:阿姆斯特丹
}

现在,如果客户想要更改关于此特定记录的某些信息,它会将新信息与版本值一起发送。在这种情况下,让我们改变学校的名称。

  {<  -  School entity  - > 
id:1,
version:0,
name:Stackoverflow,
street:Plantaanstraat,
:2,
zipCode:1234AS,
city:阿姆斯特丹
}

Hibernate随您的信息查询并添加一个额外的'where'子句来检查版本。 update ....其中id = 1和version = 0 。现在,如果该行已更新,则表示您提供了正确的版本,并且在您请求信息之间没有其他人更改过这些特定信息,并将其更改并发回。好吧?

现在如果该行没有更新?这意味着有人在你要求提供信息后正在进行快速卫生间休息时更新了这一行。这意味着你的版本已经过时了!现在需要发生的事情是真正的用例,所以我不会详细讨论这个问题:)

希望有人可以使用这些信息!



感谢所有人


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?

解决方案

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

这篇关于在spring-data项目中使用@Version的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆