hibernate stalestateException 的最佳更新策略
我们正在使用具有乐观锁定的 Hibernate。 我们所有的实体都有 @version 注释。
这工作正常,如果用户尝试保存过时的对象,我们会得到 stalestateException。 在我们的例子中,我们希望向用户提供一个通知屏幕,以放弃其更改或覆盖数据库中的当前值。
这是过时状态异常的常见用例。 我的问题与这个用例有关。如果用户决定用他的更改覆盖当前数据库行,最好的策略是什么?我已经浏览了休眠参考指南和不同的网站,但其中提到的所有事实是您必须自己捕获 stalestateException,然后以编程方式处理数据的覆盖。 我想知道 hibernate 是否有一些实用程序来简化这个策略,如果用户决定用他的数据覆盖,我可以做的最简单的事情是从数据库中检索实体的最后一个版本,然后将所有更改的字段复制到该对象并然后将更改的对象保存回数据库。 但我忍不住想知道是否没有更优雅的解决方案。
we are using hibernate with optimistic locking.
All our entities have the @version annotation.
This works fine, if a user tries to save an object that is stale we get a stalestateexception.
In our case we would like to give the user a notification screen to discard his changes or overwrite the current values in the database.
This a common use case for stale state exceptions.
My question is related to this use case. What is the best strategy if the user decides to overwrite the current database row with his changes? I've gone through the hibernate reference guide and different websites but all that is mentioned there is the fact that you have to catch the stalestateexception yourself and then programmaticaly handle the overwrite of the data.
I'm wondering if hibernate has some utilities to simplify this strategy, simplest thing i can get up with if the user decides to overwrite with his data is retrieving the last version of the entity from the database then copying all changed fields to this object and then saving the changed object back to the database.
But i can't stop wondering if there isn't a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 Hibernate 不会尝试帮助您解决此问题,因为该领域的要求可能非常复杂且定制。
我猜测,如果一个用户要保存一个已被另一个用户同时更改的对象,您很可能不想简单地加载该对象并复制所有更改的字段并撤消所有其他用户的更改。如果两个用户更改同一字段会发生什么?您可能希望向用户展示这两个版本,并要求他们决定哪个版本是正确的。有点像合并版本控制系统中的更改。
此外,您可能还具有 UI 级别验证,该验证链接了如果您只是在后端合并同一实体的两个版本并保留它,则可能会违反这些字段。
I don't think Hibernate will attempt to help you with this because requirements in this area are likely to be very complex and bespoke.
I am guessing that if a user were to save an object which has been simultaneously changed by another user you most likely won't want to simply load the object and copy all of the changed fields and undo all the other users changes. What happens if both users changed the same field? You wmight want to present the two versions to the user and ask them to decide which version is correct. A bit like merging changes in a version control system.
Also you might have UI level validation which links fields which could be violated if you simply merge two versions of the same entity in the back end and persist it.
根据我的经验,为了在这种情况下引入一些自动化,我曾经使用以下技巧。我重新加载了该实体,并将最后一个版本的值设置为之前未能保存的陈旧对象。然后我通过
merge()
传递了我的陈旧对象。这样所有字段都会被覆盖,版本是最新的并可以保存。当然,所有相关的参考文献都必须以同样的方式处理。In my experience, to introduce some automation in this case I used to do the following trick. I reloaded the entity and set the value of last version from it to my stale object which had failed to save before. Then I did
merge()
passing my stale object. That way all the fields get overwritten, the version being up-to-date and ready to save. Of course all associated references must be taken care of in the same way.