Reputation: 53
in my scenario I have 2 tables: album and image (relationship is one to many).
Here is simplified mapping:
<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>
I am doing this: 1. find object in db, 2. delete some records from collection, 3. call saveOrUpdate.
Album album = dao.getAlbum("Name of album");
Set<Image> imagesToRemove = getImagesToRemove();
album.getImages().removeAll(imagesToRemove);
dao.saveOrUpdate(album);
dao.flushAndClear();
Everything is ok till the moment that some values in table image are null. For example caption has null value.
In log I can see:
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
...
In this case delete statement won't delete any record.
What I am doing wrongly?
Upvotes: 1
Views: 82
Reputation: 1028
Actually you are not deleting orphans. When you are updating object by deleting entities they exists in database as orphans. Try using this.
<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>
Upvotes: 1