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

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

问题描述

我一直在研究带有 spring-data 的 RESTful web 服务.前几天发布了一个特殊的spring-data jpa REST框架.

I've been working on a RESTful webservice with spring-data. A few days ago a special spring-data jpa REST framework was released.

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

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?

是否可以单独使用@Version?(这样我就不必对现有的存储库/域等进行任何更改.)

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..)

我是否需要做一些额外的配置才能使用@Version?

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.

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

The annotation @Version is a javax.persistence interface and not the spring-data rest jpa framework as i mentioned earlier.

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

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;

如果您使用的是 hibernate,它将自动获取注释并创建一个版本";您(在我的情况下为 MySql)表中的列.每次更新记录时,hibernate 都会将计数器加 1.

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.

现在为什么这是你想要的东西?好吧,您可能想要使用它的原因是因为它降低了您的客户使用 stale 的机会数据.每当客户从您那里检索信息时,都会为一个版本提供他请求的数据.例如

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 提出了一个关于您的信息的查询,并添加了一个额外的where"子句来检查版本.update .... where id = 1 and version = 0.现在,如果该行已更新,则意味着您提供了正确的版本,并且在您请求信息、更改信息并将其发回期间,没有其他人更改过该特定信息.不错吧?

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!

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

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