views:

34

answers:

2

what are the minimum tables I need for many to many associations in Hibernate?

+1  A: 

Apart from the tables for the entities you want to associate, you need an association table. In its simplest form, it would contain only two foreign keys, one for each of the entities.

Péter Török
Yep. This is the way relational database works not exactly a Hibernate "limitation". Just to clarify, I'm not saying you Péter said it's a limitation.
Felipe Cypriano
A: 

By design, relational databases management systems only support one-to-many relationships. The only way to represent physically a logical many-to-many relation between A and B is thus to introduce a third junction table AB with two one-to-many relationships A -> AB <- B where the primary key of AB is formed from the two foreign keys (i.e. values that are primary keys in A and B).

So to summarize, you need 3 tables. Below an illustration of the famous Order / Product samples:

alt text

Pascal Thivent