0
0
SpringbootConceptBeginner · 4 min read

@OneToMany in JPA: Definition, Example, and Usage

@OneToMany in JPA is an annotation used to define a one-to-many relationship between two entities, where one entity owns multiple instances of another entity. It tells JPA that one object is linked to many objects of another type, like a parent having many children.
⚙️

How It Works

Imagine you have a family where one parent has many children. In JPA, @OneToMany models this kind of relationship between two entities. It means one entity (the "one" side) can be connected to many instances of another entity (the "many" side).

When you use @OneToMany, JPA knows to link the parent entity to a list or set of child entities. This helps the database understand how to store and retrieve related data efficiently. Behind the scenes, JPA usually uses a foreign key in the child table to point back to the parent.

This annotation is often paired with @ManyToOne on the child side to complete the two-way connection, making it easy to navigate from parent to children and back.

💻

Example

This example shows a Department entity that has many Employee entities. The @OneToMany annotation on the employees field tells JPA about this relationship.

java
import jakarta.persistence.*;
import java.util.List;

@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Employee> employees;

    // getters and setters
}

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;

    // getters and setters
}
Output
When saving a Department with multiple Employees, JPA stores the Department and links each Employee to it using the department_id foreign key.
🎯

When to Use

Use @OneToMany when you have a clear parent-child relationship where one entity logically owns or groups many others. For example:

  • A blog post with many comments
  • A customer with many orders
  • A school class with many students

This helps keep your data organized and makes it easy to load related data together or separately depending on your needs.

Key Points

  • @OneToMany defines a one-to-many link from one entity to many others.
  • It usually works with @ManyToOne on the other side for a bidirectional relationship.
  • The "many" side holds the foreign key in the database.
  • Use mappedBy to tell JPA which field owns the relationship.
  • Supports cascading operations to keep related data in sync.

Key Takeaways

@OneToMany maps one entity to many related entities in JPA.
It models parent-child relationships like a department with employees.
Use mappedBy to define the owning side of the relationship.
Often paired with @ManyToOne on the child entity.
Supports cascading to manage related entities automatically.