1

私のシナリオでは、アルバムと画像の 2 つのテーブルがあります (関係は 1 対多です)。

単純化されたマッピングは次のとおりです。

<hibernate-mapping>
  <class name="Album" table="album">
    <id name="id" column="album_id" type="int">
        <generator class="sequence">
            <param name="sequence">TestObject_seq</param>
        </generator>
    </id>
    <set name="image" table="image" order-by="orderBy">
        <key column="album_id" />
        <composite-element class="Image">
            <property name="caption" column="caption" type="string"/>
            <property name="path" column="path" type="string"/>
            <property name="orderBy" column="orderBy" type="int"/>
        </composite-element>
    </set>  
    </class>
</hibernate-mapping>

私はこれをやっています:1.データベースでオブジェクトを見つけ、2.コレクションからいくつかのレコードを削除し、3.saveOrUpdateを呼び出します。

Album album = dao.getAlbum("Name of album");

Set<Image> imagesToRemove = getImagesToRemove();
album.getImages().removeAll(imagesToRemove);

dao.saveOrUpdate(album);
dao.flushAndClear();

テーブル イメージの一部の値が null になるまでは、すべて問題ありません。たとえば、キャプションには null 値があります。

ログで私は見ることができます:

DEBUG SQL:292 - delete from image where album_id=? and caption=? and path=? and orderBy=?
DEBUG AbstractBatcher:343 - preparing statement
DEBUG IntegerType:59 - binding '463' to parameter: 1
DEBUG StringType:52 - binding null to parameter: 2
...

この場合、delete ステートメントはレコードを削除しません。

私が間違っていることは何ですか?

4

1 に答える 1

1

実際には、孤児を削除していません。エンティティを削除してオブジェクトを更新する場合、エンティティはデータベースに孤立として存在します。これを使ってみてください。

<set name="image" table="image" cascade="all-delete-orphan" order-by="orderBy">
    <key column="album_id" ></key>
    <composite-element class="Image">
        <property name="caption" column="caption" type="string"/>
        <property name="path" column="path" type="string"/>
        <property name="orderBy" column="orderBy" type="int"/>
    </composite-element>
 </set>  
于 2012-12-18T18:30:56.437 に答える