休眠集合未在子代移动时更新 [英] Hibernate collection not updated on child move
问题描述
我继承了休眠映射,无法将子节点从一个父节点移动到另一个父节点.我得到重复的引用,或者得到一个错误.
I'm inherited a hibernate mapping and am having trouble moving a child node from one parent node to another. Either I get a duplicate reference, or I get an error.
我在树上有位置.我想将一个叶子节点移动到另一个叶子位置.在代码中,我试图这样做:
I have locations in a tree. I want to move one leaf node to another leaf position. In code I'm trying to do this:
GeographicLocation oldParent = location.getParent();
location.setParent(newParent);
newParent.getChildren().add(location);
oldParent.getChildren().remove(location);
原因:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.test.GeographicLocation#11]
如果我删除行oldParent.getChildren().remove(location)
,则newParent
节点正确指向子级,但是oldParent
仍然还有对子级的引用(!).
If I remove the line oldParent.getChildren().remove(location)
, the newParent
node correctly points to the child, but the oldParent
still has a reference to the child as well(!).
休眠配置文件中的代码段
Snippets from hibernate config file:
<class name="GeographicLocation" table="GeographicLocation">
<id column="GeographicLocationId" name="geographicLocationId" type="java.lang.Long">
<generator class="native">
<param name="sequence">GeographicLocationId</param>
</generator>
</id>
<many-to-one class="com.test.GeographicLocation"
foreign-key="ParentFK" name="parent">
<column name="parent"/>
</many-to-one>
<bag cascade="all,delete-orphan" inverse="true" lazy="false" name="children">
<key column="parent" not-null="true"/>
<one-to-many class="com.test.GeographicLocation"/>
</bag>
我使用Hibernate的时间不长.我的理解是,作为受管理对象的location
节点在修改后会自行保存.由于休眠配置文件指定了cascade=all
,因此对集合的更改也会将更改保存到子级.但是,我似乎找不到删除旧参考文献的合法方法.有帮助吗?
I haven't been using Hibernate very long. My understanding is that the location
node, being a managed object, will save itself when modified. Since the hibernate config file specifies cascade=all
changes to the collection will also save changes to the child. However, I can't seem to find a legal way to remove the old reference. Any help?
推荐答案
我将从映射中删除delete-orphan
,因为它说一旦从集合中删除元素,就应将其删除(即显然不是您想要的).
I would remove the delete-orphan
from the mapping, since it says that as soon as you remove an element from the collection, it should be removed (which is clearly not what you want).
这篇关于休眠集合未在子代移动时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!