在spring-data项目中使用@Version

时间:2022-09-11 16:44:43

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

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

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?

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

2 个解决方案

#1


42  

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.

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

If you want to make use of @Version you need to create an version field in your domain object like so:

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

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

如果您正在使用休眠,它将自动拾取注释,它将在您的(在我的案例中为MySql)表中创建一个“版本”行。每次更新记录时,hibernate都会将计数器增加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": "*",
    "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?

Hibernate提供了一个包含您的信息的查询,并添加了一个额外的'where'子句来检查版本。更新....其中id = 1,版本= 0.现在,如果行更新,则意味着您提供了正确的版本,并且在您请求信息,更改信息并将其发回之间,没有其他人更改过该特定信息。好吗?

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

谢谢大家

#2


1  

Make sure to import import javax.persistence.Version

确保导入导入javax.persistence.Version

and not the spring one.

而不是春天的。

#1


42  

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.

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

If you want to make use of @Version you need to create an version field in your domain object like so:

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

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

如果您正在使用休眠,它将自动拾取注释,它将在您的(在我的案例中为MySql)表中创建一个“版本”行。每次更新记录时,hibernate都会将计数器增加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": "*",
    "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?

Hibernate提供了一个包含您的信息的查询,并添加了一个额外的'where'子句来检查版本。更新....其中id = 1,版本= 0.现在,如果行更新,则意味着您提供了正确的版本,并且在您请求信息,更改信息并将其发回之间,没有其他人更改过该特定信息。好吗?

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

谢谢大家

#2


1  

Make sure to import import javax.persistence.Version

确保导入导入javax.persistence.Version

and not the spring one.

而不是春天的。