简体   繁体   English

更新对 hibernate 中实体的引用的最佳方法

[英]The best way to update reference to an entity in hibernate

Imagine I have an hibernate entity that is referenced by many other entities.想象一下,我有一个被许多其他实体引用的 hibernate 实体。 Now I create new entity and I want new entity to toake place of old one so it is being referenced by others in place of the old one.现在我创建了新实体,我希望新实体取代旧实体,以便其他人引用它来代替旧实体。

What is the best way to do this?做这个的最好方式是什么?

I think you should deep copy(which ever way you preffer to do this) original entity set the state property to deleted and make hibernate to update the entity.我认为您应该深度复制(无论您喜欢哪种方式)原始实体将 state 属性设置为已删除,并使 hibernate 更新实体。 Then do whatever modification to copied entity and make hibernate to save this entity.然后对复制的实体进行任何修改并制作 hibernate 以保存该实体。 Result will be that original entity will be deleted and copied will be added as new with original references.结果将是原始实体将被删除,复制的实体将作为新实体与原始参考一起添加。

The simplest way would be to use an update statement on the referring entities, for example:最简单的方法是在引用实体上使用更新语句,例如:

update ReferringEntity re set re.entity.id = :newId where re.entity.id = :oldId

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

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