@OneToMany is the JPA annotation that maps a collection of child entities to one parent entity, such as one customer with many orders. In a bidirectional mapping it is the inverse side: you set mappedBy to the @ManyToOne field on the child, and the foreign key lives in the child table. Without mappedBy, Hibernate creates a join table or uses a join column on the parent's declaration.
The examples use Jakarta Persistence 3.x (jakarta.persistence) as in Spring Boot 3 and later. On older versions the package is javax.persistence.
@OneToMany doesmappedBy@JoinColumn@OneToMany does@OneToMany declares that one instance of an entity is related to many instances of another entity. The field is a Collection, List, Set or Map. The annotation controls four things:
mappedBy).cascade).orphanRemoval).fetch, LAZY by default).| Attribute | Purpose | Default |
|---|---|---|
mappedBy | Name of the owning field on the child | none (unidirectional) |
cascade | Operations propagated to children | none |
orphanRemoval | Delete children removed from the collection | false |
fetch | Loading strategy | LAZY |
targetEntity | Element type when generics are not used | inferred |
mappedByThis is the recommended form. The child has a @ManyToOne field, and the parent points to it.
Hibernate creates a customer_id column in orders. The mappedBy = "customer" value is the Java field name on Order. Because Order.customer owns the relationship, always set it; the addOrder helper does so for you.
@JoinColumnIf the child does not need a reference to its parent, you can map only the parent side and name the foreign key column with @JoinColumn.
This works and avoids a join table, but Hibernate inserts the child first and then runs an extra update to set customer_id. For that reason the bidirectional form with mappedBy is usually more efficient. If you omit both mappedBy and @JoinColumn, JPA maps the relationship through a join table (customer_orders), which is rarely intended.
CascadeType values are PERSIST, MERGE, REMOVE, REFRESH and DETACH, plus ALL for all of them. Typical choices:
ALL with orphanRemoval = true when children are owned by the parent (order lines, addresses).PERSIST and MERGE only when children are shared or have an independent life cycle.REMOVE from a @ManyToOne towards the parent.With orphanRemoval = true, calling customer.removeOrder(order) deletes the row when the transaction commits.
Because the collection is lazy, loading 100 customers and touching getOrders() on each runs 101 queries. Load what you need explicitly.
Do not switch the mapping to FetchType.EAGER to hide the problem; it loads the collection even when you do not need it. Do not fetch-join two or more List collections in one query, because Hibernate throws MultipleBagFetchException; use a Set or separate queries.
mappedBy, which creates an unexpected join table.null.CascadeType.ALL on a @ManyToMany or on a shared child.setOrders(newList)) on an entity with orphanRemoval = true, which throws an exception. Clear and add to the existing collection instead.@OneToMany lazy by default?Yes. @OneToMany and @ManyToMany default to FetchType.LAZY, while @ManyToOne and @OneToOne default to EAGER.
@ManyToOne with @OneToMany?No. A unidirectional @OneToMany with @JoinColumn works, but the bidirectional mapping gives better SQL and lets you navigate from child to parent.
mappedBy refer to?It refers to the field name on the child entity that owns the relationship, not to a column name or a table name.
List or Set?Use a Set if element order does not matter and you can implement equals and hashCode correctly. Use a List with @OrderBy or @OrderColumn when order matters.
@OneToMany maps the parent's collection of children. Prefer the bidirectional form with mappedBy, keep both sides in sync with helper methods, cascade from parent to child only, and fetch collections deliberately with entity graphs or fetch joins to avoid N+1 queries.