Hibernate version: 3.2.6
I am simulating Flickr's Account , Album and Collection relationships.
Account and Album is a bi-directional one-to-many relationship with cascade="save-update , delete"
Collection and Album is a bi-directional many-to-many relationship
The .hbm is :
Code:
<class name="Account">
<cache usage="nonstrict-read-write"/>
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<list name="albums" cascade="save-update , delete" inverse="false">
<cache usage="nonstrict-read-write"/>
<key column="accountId" not-null="true"/>
<list-index column="position"/>
<one-to-many class="Album"/>
</list>
</class>
<class name="Album">
<cache usage="nonstrict-read-write"/>
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<many-to-one name="account" column="accountId" not-null="true" insert="false" update="false" lazy="false"/>
<set name="collections"
table="CollectionAlbum"
cascade="save-update">
<cache usage="nonstrict-read-write"/>
<key column="albumId"/>
<many-to-many class="Collection" column="collectionId"/>
</set>
</class>
<class name="Collection">
<cache usage="nonstrict-read-write"/>
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<set name="albums"
table="CollectionAlbum"
inverse="true"
cascade="save-update">
<cache usage="nonstrict-read-write"/>
<key column="collectionId"/>
<many-to-many class="Album" column="albumId"/>
</set>
</class>
The Account-Album relationship works correctly.
The Collection-Album relationship also works correctly , I can successfully add/remove album to/from a Collection.
The problem occurs when deleting an account...
the albums belonging to the account are all automatically deleted by Hibernate ,
and the albumId in CollectionAlbum table also deleted by Hibernate.
BUT , When I retrieve a Collection , it reports :
ObjectNotFoundException: No row with the given identifier exists: [foo.bar.Album#346]
It seems Collection still 'remembers' it contains a deleted Album...
I know it can be solved by not-found="ignore" , but I feel it is ugly...
Is there any other way to solve this problem ?