-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: DeleteEventListener and ObjectDeletedException
PostPosted: Thu Jun 15, 2006 1:43 pm 
Newbie

Joined: Fri Apr 07, 2006 9:59 pm
Posts: 4
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>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 2:07 pm 
Newbie

Joined: Fri Apr 07, 2006 9:59 pm
Posts: 4
Just forgot to post the onDelete method.

Code:
public void onDelete(DeleteEvent event) throws HibernateException {
  final Object entity = event.getObject();
  if (entity instanceof Photo) {
    if(true) return;
    final Photo photo = (Photo) entity;
    final Session session = event.getSession();
    final Set<Rating> ratings = new HashSet<Rating>(photo.getRatings());
    for (Rating rating : ratings) {
      session.delete(rating);
    }

  } else if (entity instanceof Rating) {
    final Rating rating = (Rating) entity;
    rating.getPhoto().getRatings().remove(rating);
    rating.getUser().getRatings().remove(rating);
  }
}


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Thu Jun 15, 2006 3:53 pm 
Newbie

Joined: Fri Apr 07, 2006 9:59 pm
Posts: 4
I found the solution.

The problem was not that ratings were still being referenced. It was the photo itself that needed to be removed from user.getPhotos().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.