The @OneToMany annotation declares that one entity instance is associated with a collection of other entity instances, for example one Author with many Book rows. It tells JPA how to map that collection to the database, which side owns the foreign key, and how persistence operations such as persist, merge and remove cascade to the children.
In almost every real model you pair it with @ManyToOne on the child and use mappedBy, so the foreign key lives in the child table and Hibernate issues simple, predictable SQL.
In a relational database a one-to-many relationship is stored as a foreign key on the "many" side: the book table has an author_id column that points to author.id. The parent table has no column for the collection.
@OneToMany is how the parent entity exposes that relationship as a List, Set or Map field. The annotation lives in jakarta.persistence (Jakarta Persistence 3.x, used by Spring Boot 3 and Hibernate 6); older code uses javax.persistence with the same attributes.
| Attribute | Default | Meaning |
|---|---|---|
mappedBy | none | Name of the field on the child that owns the relationship |
cascade | none | Operations propagated to children (PERSIST, MERGE, REMOVE, ALL, ...) |
fetch | LAZY | When the collection is loaded |
orphanRemoval | false | Delete children removed from the collection |
targetEntity | inferred from generics | Element type, rarely needed |
The recommended mapping is bidirectional: the child owns the foreign key through @ManyToOne, and the parent refers to it with mappedBy.
Key points in this mapping:
mappedBy = "author" names the field in Book that owns the association. Hibernate writes the author_id column from Book.author, not from the collection.addBook() and removeBook() helper methods keep both sides of the association in sync. Updating only one side leaves the in-memory model inconsistent and, if you change only the inverse side, nothing is saved.@ManyToOne is declared LAZY explicitly, because @ManyToOne and @OneToOne are eager by default.new ArrayList<>()) avoids null checks.You can map the collection without a @ManyToOne on the child. By default Hibernate then creates a join table, and removing or reordering elements generates extra delete and insert statements. If you want a foreign key column on the child table instead, add @JoinColumn:
This works, but Hibernate inserts the child first and then runs a separate UPDATE to set the foreign key (unless the column is also mapped elsewhere), and the column cannot be declared NOT NULL through the association alone. For most domain models the bidirectional form is simpler and faster.
cascade controls which operations on the parent are applied to the children:
PERSIST saves new children when the parent is saved.MERGE copies detached child state when the parent is merged.REMOVE deletes children when the parent is deleted.ALL is shorthand for every operation, including REFRESH and DETACH.orphanRemoval = true additionally deletes a child row when it is removed from the parent collection, as in author.removeBook(book). Use CascadeType.ALL with orphanRemoval only when the child cannot exist without the parent (a composition, such as order lines in an order). Never use REMOVE cascade on a relationship where the children are shared or have independent meaning.
@OneToMany is lazy by default, which is the correct default. The classic performance trap is iterating over many parents and touching each collection, which executes one extra query per parent (the N+1 problem). Fix it by fetching what you need in the query:
Alternatives are an entity graph (@EntityGraph(attributePaths = "books")) or @BatchSize on the collection. Avoid FetchType.EAGER on a collection, and avoid join-fetching two sibling collections in one query, which produces a Cartesian product and a MultipleBagFetchException for lists. Pagination combined with a collection join fetch is applied in memory by Hibernate, so page the parent ids first and then fetch the collections.
A List without an index column is treated by Hibernate as a bag, which allows duplicates and has no stable order. If you need order, add @OrderBy("title asc") for ordering on read or @OrderColumn to persist the position. Use a Set when elements are unique, and implement equals() and hashCode() carefully (a business key, not the generated id alone).
They describe the same relationship from opposite sides. @ManyToOne sits on the child and owns the foreign key column, while @OneToMany sits on the parent and, with mappedBy, is only the inverse side.
Use it on the @OneToMany side of a bidirectional relationship, pointing at the field on the child that carries @ManyToOne. Without it, JPA treats the collection as its own owning side and creates a join table.
The collection was accessed after the persistence context closed. Load it inside a transaction, use join fetch or an entity graph, or map to a DTO inside the service layer.
No. REMOVE deletes children when the parent is deleted. orphanRemoval deletes a child when it is detached from the parent's collection while the parent remains.
@OneToMany maps a parent-to-children collection onto a foreign key held by the child. Prefer the bidirectional form with mappedBy and a lazy @ManyToOne, keep both sides in sync with helper methods, cascade only for true compositions, and solve N+1 queries with fetch joins, entity graphs or batch fetching.