Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 one-to-many instead of 1 many-to-many

Tags:

hibernate

In Hibernate tutorial, chapter 25 best practices says we should use 2 one-to-many relationship instead of one many-to-many with an intermediate link class. I can't see what is the benefit of it : why is it better to create a 3d entity while the many-to-many can generate a join table that is acting as this intermediate link. However this recommendation must be there for a good reason.

Can somebody explain the ground for this recommendation ? Thank you.

like image 277
user244129 Avatar asked May 21 '10 10:05

user244129


1 Answers

Many-to-many relationships quite often develop barnacles — extra data that is associated with the relationship itself, rather than either of the participants in the relationship (in my experience, this is the norm rather than the exception). For instance members and groups might be associated in a many-to-many way, and you want to know when a member joined a group, what their membership status is (new, pending, suspended, ...) and so on.

If you start by modelling the relationship directly as a many-to-many and write all your code accordingly, the very first extra column that hits the join table will break your model and a bunch of code.

like image 185
Marcelo Cantos Avatar answered Nov 07 '22 20:11

Marcelo Cantos