2

休止状態のマッピングを継承しており、ある親ノードから別の親ノードに子ノードを移動する際に問題が発生しています。重複した参照を取得するか、エラーが発生します。

ツリーに場所があります。あるリーフ ノードを別のリーフ位置に移動したい。コードで私はこれをやろうとしています:

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を長い間使用していません。私の理解ではlocation、管理対象オブジェクトであるノードは、変更されるとそれ自体を保存します。hibernate 構成ファイルcascade=allはコレクションへの変更を指定しているため、子への変更も保存されます。ただし、古い参照を削除する合法的な方法が見つからないようです。何か助けはありますか?

4

1 に答える 1

1

コレクションから要素を削除するとすぐに削除する必要があるため、マッピングから削除delete-orphanします(これは明らかにあなたが望むものではありません)。

于 2012-10-09T20:11:09.080 に答える