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.