Ok so this is probably a trivial question but I'm having trouble visualizing and understanding the differences and when to use each. I'm also a little unclear as to how concepts like uni-directional and bi-directional mappings affect the one-to-many/many-to-many relationships. I'm using Hibernate right now so any explanation that's ORM related will be helpful.
As an example let's say I have the following set-up:
public class Person{
private Long personId;
private Set<Skill> skills;
//Getters and setters
}
public class Skill{
private Long skillId;
private String skillName;
//Getters and setters
}
So in this case what kind of mapping would I have? Answers to this specific example are definitely appreciated but I would also really like an overview of when to use either one-to-many and many-to-many and when to use a join table versus a join column and unidirectional versus bidirectional.
Best Answer
Looks like everyone is answering
One-to-many
vs.Many-to-many
:The difference between
One-to-many
,Many-to-one
andMany-to-Many
is:One-to-many
vsMany-to-one
is a matter of perspective.Unidirectional
vsBidirectional
will not affect the mapping but will make difference on how you can access your data.Many-to-one
themany
side will keep reference of theone
side. A good example is "A State has Cities". In this caseState
is the one side andCity
is the many side. There will be a columnstate_id
in the tablecities
.One-to-Many
the 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_id
andaddress_3_id
or a look up table with multi column unique constraint onuser_id
onaddress_id
.Many-to-Many
members 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.