Looks like everyone is answering One-to-many vs. Many-to-many:
The difference between One-to-many, Many-to-one and Many-to-Many is:
One-to-many vs Many-to-one is a matter of perspective. Unidirectional vs Bidirectional will not affect the mapping but will make difference on how you can access your data.
- In
Many-to-onethemanyside will keep reference of theoneside. A good example is “A State has Cities”. In this caseStateis the one side andCityis the many side. There will be a columnstate_idin the tablecities.
In unidirectional,
Personclass will haveList<Skill> skillsbut
Skillwill not havePerson person. In bidirectional, both
properties are added and it allows you to access aPersongiven a
skill( i.e.skill.person).
- In
One-to-Manythe one side will be our point of reference. For example, “A User has Addresses”. In this case we might have three columnsaddress_1_id,address_2_idandaddress_3_idor a look up table with multi column unique constraint onuser_id
onaddress_id.
In unidirectional, a
Userwill haveAddress address. Bidirectional
will have an additionalList<User> usersin theAddressclass.
- In
Many-to-Manymembers of each party can hold reference to arbitrary number of members of the other party. To achieve this a look up table is used. Example for this is the relationship between doctors and patients. A doctor can have many patients and vice versa.