Java Hibernate集合在子移动时未更新
我继承了hibernate映射,无法将子节点从一个父节点移动到另一个父节点。要么我得到一个重复的引用,要么我得到一个错误 我在树上有位置。我想将一个叶节点移动到另一个叶位置。在代码中,我尝试这样做:Java Hibernate集合在子移动时未更新,java,hibernate,Java,Hibernate,我继承了hibernate映射,无法将子节点从一个父节点移动到另一个父节点。要么我得到一个重复的引用,要么我得到一个错误 我在树上有位置。我想将一个叶节点移动到另一个叶位置。在代码中,我尝试这样做: GeographicLocation oldParent = location.getParent(); location.setParent(newParent); newParent.getChildren().add(location); oldParent.getChildren().rem
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
仍然有一个对子节点的引用(!)
hibernate配置文件中的代码段:
<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的时间不长。我的理解是,
位置
节点作为一个托管对象,在修改时会保存自身。因为hibernate配置文件指定了cascade=all
对集合的更改也将保存对子集合的更改。然而,我似乎找不到合法的方法来删除旧的引用。有什么帮助吗?我会从映射中删除删除孤立项,因为它说一旦从集合中删除元素,就应该删除它(这显然不是您想要的)。似乎是以下内容的重复: