Edson Tirelli
Edson Tirelli

Reputation: 3901

JPA mapping of multiple attributes of the same type

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.

Upvotes: 0

Views: 809

Answers (1)

JB Nizet
JB Nizet

Reputation: 692073

The fact that you have two instances doesn't change anything.

You map dad and mom each as a ManyToOne association, and there will be two join columns in the family table: one for dad and one for mom.

You map children as 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).

Upvotes: 3

Related Questions