2012-10-09 58 views
2

我繼承了hibernate映射,並且無法將子節點從一個父節點移動到另一個父節點。要麼我得到一個重複的參考,或者我得到一個錯誤。休眠集合未在子移動中更新

我在樹中有位置。我想將一個葉子節點移動到另一個葉子位置。在代碼中,我試圖做到這一點:

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仍然具有參考孩子也是(!)。從休眠配置文件

摘錄:

<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集合的更改也會將更改保存到子級。但是,我似乎無法找到合法的方式來刪除舊的參考。任何幫助?

+0

似乎重複的:http://stackoverflow.com/questions/11649249/deleted-object-would-be-re-saved-by-cascade-remove-deleted-object-from-associat – dan

回答

1

我會從映射中刪除delete-orphan,因爲它說,只要從集合中刪除元素,它應該被刪除(顯然不是你想要的)。