Hibernate版本化父母实体 [英] Hibernate versioning parent entity

查看:100
本文介绍了Hibernate版本化父母实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两个实体Parent和Child。


  • Child是Parent的临时集合的一部分
  • 子对象具有与FetchType进行父对象的ManyToOne映射。 LAZY



两者都以同样的形式显示给用户。当用户保存数据时,我们首先更新Parent实例,然后更新Child集合(均使用合并)。



现在出现棘手的部分。当用户只修改表单上的Child属性时,hibernate脏检查不会更新父实例,因此不会增加该实体的乐观锁定版本号。



我希望看到只有Parent被版本化的情况,并且每次我为Parent调用合并时,即使实际更新未在db中执行,版本也会始终更新。 / p>

解决方案

我想我明白了。合并后被调用一个附加的实例引用被返回。当我使用entityManager.lock(更新,LockModeType.WRITE)获得显式锁定时;那么即使父实例未在db中更新,版本号也会增加。

另外,我将分离的实例版本与持久实例版本进行比较。如果它们不匹配,那么Parent在db中被更新,并且版本号也被更改。这使版本号保持一致。否则,即使合并操作改变了它,entityManager.lock也会增加版本号。



仍然在寻找解决方案,当合并时实体不脏时,如何让hibernate增加版本。


Consider two entities Parent and Child.

  • Child is part of Parent's transient collection
  • Child has a ManyToOne mapping to parent with FetchType.LAZY

Both are displayed on the same form to a user. When user saves the data we first update Parent instance and then Child collection (both using merge).

Now comes the tricky part. When user modifies only Child property on the form then hibernate dirty checking does not update Parent instance and thus does not increase optimistic locking version number for that entity.

I would like to see situation where only Parent is versioned and every time I call merge for Parent then version is always updated even if actual update is not executed in db.

解决方案

I think I figured it out. After merge is called an attached instance reference is returned. When I obtain an explicit lock for that using entityManager.lock(updated, LockModeType.WRITE); then version number is increased even if Parent instance was not updated in db.

In addition I am comparing detached instance version with persisted instance version. If they don't match then Parent was updated in db and also version number has changed. This keeps version numbers consistent. Otherwise entityManager.lock would increase version number even if merge operation changed it.

Still looking for solution how to make hibernate increase version when entity is not dirty during merge.

这篇关于Hibernate版本化父母实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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