Show TOC

Background documentationRelationships Locate this document in the navigation structure

 

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

Example Example

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.

End of the example.

Syntax Syntax

  1. @Entity
    public class Employee {
    	@Id
    	private int id;
    	// ...
    	@OneToOne
    	private Phone phone;
    	// ...
    }
End of the code.
One-to-Many Relationships

Example Example

One department employs many employees. An employee works in one department only.

End of the example.

Syntax Syntax

  1. @Entity
    public class Department {
    	@Id
    	private int id;
    	// ...
    	@OneToMany
    	private Set<Employee> employees;
    	// ...
    }
End of the code.
Many-to-One Relationships

Example Example

Many employees work at a department. Each employee works in one department only.

End of the example.

Syntax Syntax

  1. @Entity
    public class Employee {
    	@Id
    	private int id;
    	// ...
    	@ManyToOne
    	private Department department;
    	// ...
    }
End of the code.
Many-to-Many Relationships

Example Example

An employee participates in many projects. Each project involves many employees.

End of the example.

Syntax Syntax

  1. @Entity
    public class Employee {
    	@Id
    	private int id;
    	// ...
    	@ManyToMany
    	private Set<Project> projects;
    	// ...
    }
End of the code.
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.