What is the difference between @OneToMany and @ManyToOne?
In Java Persistence API (JPA), @OneToMany and @ManyToOne are two fundamental annotations used to define relationships between entities. They represent the two sides of a many-to-one or one-to-many association in a relational database, indicating how one entity relates to multiple instances of another entity, and vice versa.
@OneToMany Annotation
The @OneToMany annotation is used on the "one" side of a one-to-many relationship. It signifies that one instance of the current entity can be associated with multiple instances of another entity. For example, a single Department entity can have many Employee entities. This annotation is typically placed on a collection field in the "one" side entity.
- Represents the 'one' side of the relationship.
- Maps to a collection (e.g., List<Employee>, Set<Product>) of the target entity.
- Foreign key is usually defined on the 'many' side's table (often managed via
mappedBy). - Default fetch type is LAZY.
- Often accompanied by the 'mappedBy' attribute to indicate the owning side of the relationship on the 'many' side.
@ManyToOne Annotation
The @ManyToOne annotation is used on the "many" side of a many-to-one relationship. It indicates that multiple instances of the current entity can be associated with a single instance of another entity. For example, many Employee entities can belong to one Department entity. This annotation is typically placed on a single-valued field in the "many" side entity.
- Represents the 'many' side of the relationship.
- Maps to a single instance of the target entity.
- Foreign key is typically defined directly on the current entity's table (the entity where @ManyToOne is declared).
- Default fetch type is EAGER.
- This side is usually the owning side of the relationship.
Key Differences Summary
| Feature | @OneToMany | @ManyToOne |
|---|---|---|
| Relationship Direction | One entity instance maps to multiple target entity instances. | Multiple entity instances map to a single target entity instance. |
| Field Type | Collection (e.g., List<Child>, Set<Child>) | Single entity instance (e.g., Parent) |
| Foreign Key (FK) Location | Typically on the 'many' side's table (managed via `mappedBy` attribute). | Directly on the 'many' side's table (where @ManyToOne is declared). |
| Owning Side | Often the non-owning side when `mappedBy` is used. | Typically the owning side of the relationship. |
| Default Fetch Type | LAZY | EAGER |
| Cardinality | One parent, many children. | Many children, one parent. |
Example Scenario: Department and Employee Entities
Department Entity (One side of @OneToMany)
public class Department {
@Id @GeneratedValue
private Long id;
private String name;
// One Department has Many Employees
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Employee> employees = new ArrayList<>();
// ... getters and setters
}
Employee Entity (Many side of @ManyToOne)
public class Employee {
@Id @GeneratedValue
private Long id;
private String name;
// Many Employees belong to One Department
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id") // Specifies the foreign key column
private Department department;
// ... getters and setters
}
In this example, the Department entity (the 'one' side) uses @OneToMany on its employees collection field, indicating that a department can have many employees. It uses mappedBy="department" to denote that the department field in the Employee entity is the owning side. The Employee entity (the 'many' side) uses @ManyToOne on its department field and @JoinColumn to specify the foreign key (department_id) in the Employee table that references the Department table.