Hibernate 3.1rc2
My domain model has a User, a Photo and Rating entity.
A User can have Photo(s) and can rate any Photo that he does not own.
The model is:
User(1..*)Photo
User(1..*)Rating(*..1)Photo
I get a ObjectDeletedException:when I try to delete a photo:
=> org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [hub.domain.Photo#25]
To solve the problem I tried to implement a DeleteEventListener that deletes all ratings from a photo and remove these rating from the related collections.
After the my custom DeleteEventListener has complemented its task I can see that the ratings are removed from all related collections but I still get the ObjectDeletedException.
I have tried many things with no luck.
Any idea?
Code:
<hibernate-mapping>
...
<class name="User" table="users">
<set name="photos" inverse="true" cascade="all,delete-orphan">
<key column="user_id"/>
<one-to-many class="Photo"/>
</set>
<set name="ratings" inverse="true" cascade="save-update">
<key column="user_id"/>
<one-to-many class="Rating"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="Photo" table="photos" >
...
<many-to-one name="user" class="User" column="user_id" not-null="true"/>
<set name="ratings" inverse="true" cascade="save-update">
<key column="photo_id"/>
<one-to-many class="Rating"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Rating" table="ratings">
...
<many-to-one name="photo" class="Photo" column="photo_id" not-null="true" unique-key="fk"/>
<many-to-one name="user" class="User" column="user_id" not-null="true" unique-key="fk"/>
</class>
</hibernate-mapping>