<legend id='Wz5fm'><style id='Wz5fm'><dir id='Wz5fm'><q id='Wz5fm'></q></dir></style></legend>
  • <i id='Wz5fm'><tr id='Wz5fm'><dt id='Wz5fm'><q id='Wz5fm'><span id='Wz5fm'><b id='Wz5fm'><form id='Wz5fm'><ins id='Wz5fm'></ins><ul id='Wz5fm'></ul><sub id='Wz5fm'></sub></form><legend id='Wz5fm'></legend><bdo id='Wz5fm'><pre id='Wz5fm'><center id='Wz5fm'></center></pre></bdo></b><th id='Wz5fm'></th></span></q></dt></tr></i><div id='Wz5fm'><tfoot id='Wz5fm'></tfoot><dl id='Wz5fm'><fieldset id='Wz5fm'></fieldset></dl></div>

      1. <tfoot id='Wz5fm'></tfoot>
      2. <small id='Wz5fm'></small><noframes id='Wz5fm'>

          <bdo id='Wz5fm'></bdo><ul id='Wz5fm'></ul>

        在 spring-data 项目中使用 @Version

        Using @Version in spring-data project(在 spring-data 项目中使用 @Version)
          <tbody id='DY37K'></tbody>
        • <bdo id='DY37K'></bdo><ul id='DY37K'></ul>

            • <i id='DY37K'><tr id='DY37K'><dt id='DY37K'><q id='DY37K'><span id='DY37K'><b id='DY37K'><form id='DY37K'><ins id='DY37K'></ins><ul id='DY37K'></ul><sub id='DY37K'></sub></form><legend id='DY37K'></legend><bdo id='DY37K'><pre id='DY37K'><center id='DY37K'></center></pre></bdo></b><th id='DY37K'></th></span></q></dt></tr></i><div id='DY37K'><tfoot id='DY37K'></tfoot><dl id='DY37K'><fieldset id='DY37K'></fieldset></dl></div>
                1. <legend id='DY37K'><style id='DY37K'><dir id='DY37K'><q id='DY37K'></q></dir></style></legend>

                  <small id='DY37K'></small><noframes id='DY37K'>

                2. <tfoot id='DY37K'></tfoot>
                  本文介绍了在 spring-data 项目中使用 @Version的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在使用 spring-data 开发 RESTful Web 服务.前几天发布了一个特殊的 spring-data jpa REST framework.

                  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;
                  

                  如果您使用休眠,它会自动拾取注释并创建一个版本";您(在我的情况下为 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"子句来检查版本.更新 .... 其中 id = 1 和 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                        <small id='RZwW1'></small><noframes id='RZwW1'>

                            <bdo id='RZwW1'></bdo><ul id='RZwW1'></ul>
                              <tbody id='RZwW1'></tbody>

                            <i id='RZwW1'><tr id='RZwW1'><dt id='RZwW1'><q id='RZwW1'><span id='RZwW1'><b id='RZwW1'><form id='RZwW1'><ins id='RZwW1'></ins><ul id='RZwW1'></ul><sub id='RZwW1'></sub></form><legend id='RZwW1'></legend><bdo id='RZwW1'><pre id='RZwW1'><center id='RZwW1'></center></pre></bdo></b><th id='RZwW1'></th></span></q></dt></tr></i><div id='RZwW1'><tfoot id='RZwW1'></tfoot><dl id='RZwW1'><fieldset id='RZwW1'></fieldset></dl></div>
                            <legend id='RZwW1'><style id='RZwW1'><dir id='RZwW1'><q id='RZwW1'></q></dir></style></legend><tfoot id='RZwW1'></tfoot>