-->
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.  [ 4 posts ] 
Author Message
 Post subject: Delete an object inside a graph of objects
PostPosted: Thu Oct 27, 2005 8:29 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
I am having trouble with deleting an object that has bidirectional connections to a few other objects. Before tracking down the cause of particular errors, I'd like to decide on the right method how to achieve this.

Let's say I have Persons, Books, and Roles. A person may play several roles to one or different books, and a book may be connected to many persons via roles. Every role refers to exactly one person and one book. So the associations are:
    Person <-->> Role <<--> Book
I use bidirectional pointers between my classes. Adding roles, connecting them to persons and books, storing them to the database and retrieving them appears to look just fine.

The question is about deleting a role. If the user wants to remove a role object connecting a person and a book, I see two different approaches to do this:
    1. Issuing a delete(role) to Hibernate, and trying to get Hibernate to not only delete the role from the database, but also to adjust the sets of references in the person and book object. Of course, I don't want the person or the book deleted, so it doesn't appear like a matter of cascading to me.

    2. Removing all references to the role, i.e. removing them from the roles collections of the person and book object, and then trying to get Hibernate to update the graph, this time in a cascading way, to realize one object is missing, and to delete it.

Which of these methods works (if coded properly), will never work, or is the preferred one?

If this is a stupid question, I apologize in advance. However, the literature about Hibernate isn't too talkative when it comes to deleting things.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 10:18 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
Number 2 is the correct way to go about it. I've generally only used delete to get rid of instance that have they own life cycle seperate from all the objects they're associated to. In this case a role would never exist when it's no longer refernced so cascaded deletes seems the most logical course of action.

In addition, even if you do use a explicit delete you still need to remove all the associations the object participates in.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 6:24 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Oh, obviously I didn't describe my application precisely enough. The role does have a life cycle of its own. In the function I describe, the role is removed, although the book and author stay alive happily. The only thing that should happen is that the book and author lose their pointers to the role.

When I do delete and "forget" removing the associations from other objects to the deleted object -- where will these associations point to?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 6:23 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
I beleive you get a exception when the session flushes. In a lot of my models I have cascade all for most of the associations and I remember getting an exception if i deleted a object explicitly but left it referenced by persistent objects. Something about trying to delete and save in the same transaction. I assume you'd get some kind of exception even without cascade but I can't say as I've ever done it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.