I am learning JPA (2.0) and I would like to understand how to properly map multiple attributes of the same class type. For instance, pretend I have a model:
@Entity
class Person {
String name;
int age;
// getters/setters
}
@Entity
class Family {
Person dad;
Person mom;
List<Person> children;
// getters/setters
}
How can I properly map mom and dad attributes?
Thanks and sorry if it is too basic. Couldn’t find an answer anywhere.
The fact that you have two instances doesn’t change anything.
You map
dadandmomeach as a ManyToOne association, and there will be two join columns in the family table: one for dad and one for mom.You map
childrenas a OneToMany (assuming a child can only be a child in one family), and there will be either a join table between Family and Person (the default for a unidirectional OneToMany), or a join column in the Person table referencing the family table (the default for a OneToMany bidirectional association).