Hi all,
I am having a problem with many-to-many relationships. I have seen a few posts about this, but I have not seen a definitive "best practices" answer.
Here is the problem:
I have a many to many relationship betwen 2 classes (lets call them A and B). I set inverse=true on A so that B is responsible for persisting hte relationship. This is great when I delete B, the entries in the join table go away. But when I delete 'A' they do not get automatically erased.
I can get around this a few different ways from what I've seen:
1) write a remove() function similar to the add() function that will ensure that delete gets removed from both sides.
2) if I am using a db that supports it, do a on-delete-cascade
3) (not really a solution) set the hibernate property on the many-to-many relationship to ignore invalid foreign keys.
Solution #1 in particular works well, and feels like the "proper" way to do things, but I run into a problem if I have a class C that has one-to-many association with class 'A'. Lets says that this collection is set to on-delete-orphan, so that the lifecycle of 'A' is dependant on 'C.'
Now when I delete 'C', it automatically removes 'A' which does not automatically remove 'B' from the link tables. (and it does not call the remove() function I wrote) so the foreign key problem is back.
Anyway.. I'm sure this is a pretty common thing that people see, I was just curious to see how you guys get around it.
thanks!
|