简体   繁体   English

安全更新休眠实体的最佳方法是什么?

[英]What is the best way to safely update hibernate entity?

First to explain the context. 首先解释一下上下文。 I have backend Java (Spring/Hibernate) application that I access via JMS. 我有可以通过JMS访问的后端Java(Spring / Hibernate)应用程序。 I have client app (RESTfull) that I access via Url. 我有可通过网址访问的客户端应用(RESTfull)。 I have a complex entity with more than one list (most of it are lazy) and this entity is composition of xy other entities. 我有一个复杂的实体,其中有多个列表(大多数是惰性的),并且这个实体是xy其他实体的组成。

Problem: Since I access it via URL, I create Java object in client app from URL parameters. 问题:因为我通过URL访问它,所以我在URL地址参数的客户端应用程序中创建Java对象。 I'm sending it to backend via JMS, but on the backEnd, I do not have Hibernate object, so I cant simple merge it. 我通过JMS将其发送到后端,但是在后端,我没有Hibernate对象,因此无法简单地合并它。

I can go trough all that came from client like: 我可以浏览所有来自客户的信息,例如:

  • get hibernate object by id 通过ID获取休眠对象
  • check what is different 检查有什么不同
  • set new values 设定新的价值
  • update 更新

and repeat it for every composition entity, but I'm wondering if there is more elegant and "easy to maintain" way to update this entity with all changes. 并针对每个合成实体重复此操作,但我想知道是否存在更优雅且“易于维护”的方式来通过所有更改来更新此实体。

I hope I explained it well. 我希望我能解释清楚。 Thanks in advance! 提前致谢!

From your description it seem the steps you follow are right. 从您的描述看来,您遵循的步骤是正确的。 The first(get object) and last(update object) steps are inevitable. 第一步(获取对象)和最后一步(更新对象)是不可避免的。 The only place where you can optimize is the checking/setting part. 唯一可以优化的地方是检查/设置部分。 For that you can write a generalized method which accepts two objects and compares them for changes using Reflection. 为此,您可以编写一个通用方法,该方法接受两个对象并使用Reflection比较它们的更改。 This way it can be reused again. 这样,它可以再次重用。

Here is a sample code using reflection. 这是使用反射的示例代码 Change it to suit your need 根据您的需要进行更改

If you are confident that you have had the latest values for modified entity fields (eg by keeping an optimistic-lock version field) and/or you intend to just overwrite all of the modified fields, there is no need to check if the fields are changed or not. 如果您确信已拥有修改后的实体字段的最新值(例如,通过保留乐观锁定版本字段)和/或打算覆盖所有修改后的字段,则无需检查这些字段是否为改变与否。 Just find the entities and set the fields that you have (including ID and version) and Hibernate will overwrite your changes, if any, to the database unless an entity has been modified by some other thread in the meantime. 只需找到实体并设置您拥有的字段(包括ID和版本),Hibernate就会将对数据库的更改(如果有的话)覆盖到数据库,除非在此期间某个实体已被其他线程修改。 In this case, the update/merge fails by an optimistic lock exception. 在这种情况下,由于乐观锁异常,更新/合并失败。

So the steps would become: 因此,步骤将变为:

  • Find Hibernate object by id 通过ID查找Hibernate对象
  • Set new/modified values including version 设置新的/修改的值,包括版本
  • Update/merge/persist 更新/合并/持久

Of course, you need to make sure you have proper cascades on your entity relationships or update entities one by one for every related one. 当然,您需要确保您的实体关系上具有适当的级联,或者对于每个相关的实体一个一个地更新实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM