-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-many best practices
PostPosted: Fri Feb 09, 2007 6:15 pm 
Newbie

Joined: Fri May 14, 2004 5:38 pm
Posts: 12
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!


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

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.