Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Hibernate docs recommend to use a join table for a one-to-many relation?

I thought that the common way to model a one-to-many relation in a database is via a foreign key relationship (i.e. one customer with many orders -> order table gets a FK reference to customer table).

However, Hibernate recommends to use a join table to model such relationships:

A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section). This kind of association is described through a @JoinColumn.

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-association

Why is this "not really recommended". I thought the using an FK was the standard, and that join tables are only used for many-to-many relationships?

I don't like to create a join table, because the data model will look like the relationship is many-to-many, when in fact it is one-to-many.

What is the reason for this recommendation in the Hibernate docs?

like image 637
sleske Avatar asked Feb 03 '23 16:02

sleske


1 Answers

This question has been asked and answered on the Hibernate forums: https://forum.hibernate.org/viewtopic.php?t=954178&highlight=unidirectional+null+foriegn+foreign+key. It appears to be less an issue fo DB design and more one of how Hibernate functions.

like image 168
atrain Avatar answered Feb 05 '23 16:02

atrain