Entities are plain old Java objects. Java fields or properties are used to establish relationships between entities. These are called relationship attributes .
According to cardinality, there are four types of relationships between entities: one-to-one, one-to-many, many-to-one, and many-to-many. Relationships are always polymorphic.
JPA supports both unidirectional and bidirectional relationships. In contrast to EJB 2.1, bidirectional relationships are not automatically managed by the container but must be explicitly managed on both sides by the application.
One side is the owning side of the relationship and the other side is the inverse side. The owning side determines the update behavior of the relationship at runtime. A unidirectional relationship has only an owning side.
Relationships Specification
To specify a relationship between two entities, you annotate the attribute of the entity that use annotations within the entity class. The following table shows the annotations used to define a relationship:
Relationship Type |
Annotation |
---|---|
One-to-one |
OneToOne |
One-to-many |
OneToMany |
Many-to-one |
ManyToOne |
Many-to-many |
ManyToMany |
Unidirectional relationships have one side only, which is the owning side, naturally.
Additionally, the following rules must be applied when defining a bidirectional relationship:
The inverse side refers to the owning side by means of the mappedBy element of the relationship annotation. This element denotes the field or property that is the owner of the relationship.
The owning side of the one-to-many and many-to-one relationships must be the many side. Therefore, the ManyToOne annotation must not contain the mappedBy element.
The owning side of a one-to-one relationship contains the relevant foreign key.
The owning side of the many-to-many relationships is either of the two many sides.
To override the default object-relational mapping values, you use additional annotations. For example, column, table, and foreign key annotations.
One-to-One Relationships
In this example, an employee has a phone which is not shared by other employees. Therefore, the relationship from the Employee entity to the Phone entity is single-valued (has a cardinality one). It is specified using the relationship phone of the entity Employee . The Java type of the relationship field phone is the Java type of the related entity Phone .
The annotation @OneToOne indicates that only one Employee refers to a given Phone , and an Employee refers to only one Phone .
@Entity public class Employee { @Id private int id; // ... @OneToOne private Phone phone; // ... }
One-to-Many Relationships
One department employs many employees. An employee works in one department only.
@Entity public class Department { @Id private int id; // ... @OneToMany private Set<Employee> employees; // ... }
Many-to-One Relationships
Many employees work at a department. Each employee works in one department only.
@Entity public class Employee { @Id private int id; // ... @ManyToOne private Department department; // ... }
Many-to-Many Relationships
An employee participates in many projects. Each project involves many employees.
@Entity public class Employee { @Id private int id; // ... @ManyToMany private Set<Project> projects; // ... }
Object-Relational Mapping Defaults
If you do not specify the object-relational mapping attributes explicitly, the default rules apply. For more information, refer to the Java Persistence API specification at http://java.sun.com .