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.  [ 5 posts ] 
Author Message
 Post subject: why is many-to-many bad?
PostPosted: Wed Nov 19, 2008 7:34 am 
Newbie

Joined: Wed Nov 19, 2008 7:08 am
Posts: 1
I'm currently reading "Java Persistence with Hibernate" and the author encourages the reader on multiple occasions to not use many-to-many mappings and that entire enterprise applications can be written with only one-to-many / many-to-one mappings.

I'm curious .. what exactly is it with many-to-many that's so bad? Why should it be avoided? And how would you map a classic many-to-many usecase (a person can attend multiple events, an event can have multiple participants) with just one-to-many / many-to-one?


Top
 Profile  
 
 Post subject: Re: why is many-to-many bad?
PostPosted: Fri Jun 26, 2009 4:02 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
Actualy, I was also just condering why many-to-many should be avoided.

Could anyone explain?

tx,
Stijn


Top
 Profile  
 
 Post subject: Re: why is many-to-many bad?
PostPosted: Fri Jun 26, 2009 4:54 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
It's not something that standard SQL supports.

Consequences are:

1) Hibernate has to work harder to make it happen. Hibernate needs to execute more code (increasing the probability of hitting a bug), which is less well tested (again increasing the probability of hitting a bug).

2) If something goes wrong, you'll have more trouble finding the cause (because Hibernate is executing more and more intricate code).

3) More importantly, the Java data model is farther removed from the database model. This makes it harder to check that the models still match, both for you and for automatic validation.

4) Most importantly, experience suggests that most many-to-many associations get turned into two one-to-many associations anyway.
Say, you have persons and clubs and what to track who's a Member of what Club - that means a many-to-many relationship. However, more often than not you'll find that you want to track not just the fact that a person is a member of a club, but associated data as well - such as date of joining, membership fee, whether that fee is outstanding, etc. etc. So you need Membership objects, which will have one-to-many relationships with Member and Club. I.e. the link between Member and Club acquired attributes and had to be turned from "link" to "object", requiring you to restructure your code everywhere you access clubs from members or vice versa.
What follows is that a many-to-many association is ok if you can safely exclude that you'll ever want to associate attributes with a link, and use two one-to-many associations otherwise to make the data model a bit more stable (it will change far too much anyway).
(For one-to-many associations, if the link acquires an attribute, you can place the attribute in the object on the "many" side of things.)


Top
 Profile  
 
 Post subject: Re: why is many-to-many bad?
PostPosted: Fri Jun 26, 2009 5:03 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
Excellent explanation, tx a lot!


Top
 Profile  
 
 Post subject: Re: why is many-to-many bad?
PostPosted: Fri Jun 26, 2009 5:13 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
You're welcome :)

Actually I'm not sure whether I covered all aspects.
Many-to-many relationships are such a basic "you just don't do that" concept that it's actually hard to remember the exact reasons.
So: if anybody has practical experience with the trade-offs around many-to-many relationships, I'll be interested.


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