The @OneToMany annotation maps a one-to-many relationship between two JPA entities: one parent row is related to many child rows, such as one Author with many Book records. It tells the persistence provider (Hibernate, EclipseLink) that a collection field on the parent is backed by a foreign key on the child table or by a join table, and it controls cascading, fetching and orphan removal for that collection.
In practice you almost always pair it with @ManyToOne on the child and mark the parent side with mappedBy, so the child owns the foreign key. This article uses the jakarta.persistence package (Jakarta Persistence 3.x, used by Spring Boot 3 and Hibernate 6). On older stacks (Spring Boot 2, Hibernate 5) the same annotations live in javax.persistence.
@OneToMany is placed on a collection-valued attribute (List, Set, Collection or Map). It has these main attributes:
| Attribute | Purpose | Default |
|---|---|---|
mappedBy | Names the field on the child that owns the relationship | none (unidirectional) |
cascade | Operations propagated to children (PERSIST, MERGE, REMOVE, REFRESH, DETACH, ALL) | none |
fetch | LAZY or EAGER loading of the collection | LAZY |
orphanRemoval | Delete children removed from the collection | false |
targetEntity | Element type when generics are not available | inferred |
The default fetch type is LAZY, so the children are loaded only when you first touch the collection. Leave it that way. Switching to EAGER makes every load of the parent also load all children, which quickly becomes a performance problem.
The recommended mapping has two sides. The child (@ManyToOne) is the owning side because it holds the foreign key column. The parent (@OneToMany(mappedBy = ...)) is the inverse side.
The mappedBy = "author" value must match the field name on Book. Hibernate creates a single author_id column on the book table. The helper methods addBook and removeBook keep both sides of the association in sync in memory, which JPA does not do for you. Only the owning side (Book.author) is used to write the foreign key, so forgetting to set it means the column stays NULL.
You can map @OneToMany without a @ManyToOne on the other side. Without @JoinColumn, Hibernate defaults to a join table (author_books) that holds pairs of ids, which costs an extra table and extra SQL.
With @JoinColumn the foreign key lives on book, but Hibernate typically inserts the child first and then issues a separate UPDATE to set author_id. This works, but the bidirectional mapping above produces simpler SQL, so prefer it when you control both entities.
cascade = CascadeType.ALL saves, merges and deletes children together with the parent. orphanRemoval = true additionally deletes a child row when you remove it from the collection.
Use cascading only for true parent-child ownership, where the child has no life outside the parent. Do not cascade REMOVE onto entities that other parents may share.
Because the collection is lazy, looping over many authors and reading getBooks() issues one query for the authors plus one per author. Fix it by fetching what you need explicitly.
Notes:
join fetch a collection together with pagination; Hibernate then paginates in memory and logs a warning. Fetch the page of ids first, or use @BatchSize or hibernate.default_batch_fetch_size.List collections at once throws MultipleBagFetchException. Use Set or fetch them in separate queries.LazyInitializationException. Load what the view needs inside the service layer or return DTOs.List for ordered or small collections and Set when elements are unique, but implement equals and hashCode carefully (a business key or a stable id strategy).new ArrayList<>()) so it is never null.@Data or @ToString on entities with bidirectional links; the generated methods recurse between parent and child.author_id) if your database does not create one automatically.mappedBy marks the inverse side and says another field owns the mapping. @JoinColumn is placed on the owning side to name the foreign key column.
Lazy. @ManyToOne and @OneToOne are eager by default, but collection associations are lazy.
Use it when a child cannot exist without its parent and removing it from the collection should delete the row, for example order lines in an order.
No, but a bidirectional mapping with mappedBy generates cleaner SQL and is the usual recommendation.
@OneToMany models a parent with many children. Prefer a bidirectional mapping where the @ManyToOne side owns the foreign key and the @OneToMany side uses mappedBy, keep the default lazy fetching, cascade only for true ownership, and use join fetch or entity graphs to avoid N+1 queries.