@ManyToOne maps the "many" side of a relationship: it puts the foreign key column in the child table and is the owning side. @OneToMany maps the collection on the "one" side, and in a bidirectional mapping it uses mappedBy to point back to the @ManyToOne field. Together they model one parent with many children, such as one department with many employees.
The examples use Jakarta Persistence 3.x (jakarta.persistence), which Spring Boot 3 and later and Hibernate 6 use. On older stacks, replace the package with javax.persistence.
@ManyToOne@OneToMany
A one-to-many relationship in a relational database is stored in exactly one place: a foreign key column in the table of the "many" side. That is why @ManyToOne is the annotation that really maps a column, and why @OneToMany is, in a bidirectional mapping, only a read-only view of the same data.
| Aspect | @ManyToOne | @OneToMany |
|---|---|---|
| Placed on | Child entity (Employee) | Parent entity (Department) |
| Field type | A single entity | A collection (List, Set) |
| Database effect | Creates the foreign key column | None when mappedBy is set |
| Owning side | Yes | No (inverse side) |
| Default fetch | EAGER | LAZY |
The owning side is the side whose state Hibernate writes to the database. If you change only the inverse side (adding an employee to department.getEmployees() without setting employee.setDepartment(...)), nothing is persisted for that relationship.
@ManyToOneThe child entity holds the reference to its parent and declares the foreign key with @JoinColumn.
This produces an employee table with a non-null department_id column that references department(id). Two details matter in practice:
@ManyToOne is EAGER by default. Set fetch = FetchType.LAZY explicitly so loading an employee does not always load its department.optional = false together with nullable = false documents and enforces that every employee belongs to a department.@OneToManyThe parent entity declares the collection and names the field on the child that owns the relationship.
mappedBy = "department" is the name of the field in Employee, not the column name. Without mappedBy, Hibernate treats @OneToMany as its own owning side and creates an extra join table, which is rarely what you want.
Because only Employee.department is written to the database, both sides of the association must be kept in sync in memory. The addEmployee and removeEmployee helpers do that in one place, so callers cannot forget half of the update. Initialise the collection (new ArrayList<>()) so it is never null.
The cascade attribute propagates entity state transitions from parent to children. CascadeType.ALL includes persist, merge, remove, refresh and detach. With it, saving a department also saves its new employees.
orphanRemoval = true deletes a child row when it is removed from the parent's collection. It is the right choice when the child cannot exist without the parent, as with order lines in an order. Do not cascade remove from a child to a parent: put cascade on the @OneToMany side only.
With Spring Data JPA, saving the parent persists the children because of the cascade.
Hibernate then issues three inserts: one for the department and one for each employee, with department_id set from the owning side.
employees is lazy, so reading it for many departments triggers one query per department. Fetch the collection explicitly when you need it:
For lists of departments, prefer @EntityGraph or batch fetching (hibernate.default_batch_fetch_size) over eager mappings.
You do not have to map both sides. Two common variants:
@ManyToOne on the child. This is the simplest and most efficient mapping; query children with findByDepartmentId(...).@OneToMany on the parent with @JoinColumn(name = "department_id"). This works, but Hibernate may issue extra update statements to set the foreign key after inserting the child.Avoid @OneToMany without mappedBy and without @JoinColumn, because it generates a join table.
@OneToMany without @ManyToOne?Yes. Add @JoinColumn on the @OneToMany field to make it a unidirectional mapping. It works but is less efficient than the bidirectional form, because of the extra update statements.
The side without mappedBy, which is the @ManyToOne side in a bidirectional one-to-many mapping. Only changes to the owning side are written to the foreign key column.
A bidirectional mapping creates a cycle (department to employees to department). Return DTOs or records from your controllers instead of entities, or break the cycle with @JsonIgnore or @JsonManagedReference and @JsonBackReference.
List or Set for the collection?Either works. A Set avoids duplicates and some Hibernate multiple-bag fetch problems, but then you must implement equals and hashCode correctly on the child.
@ManyToOne creates the foreign key and owns the relationship; @OneToMany(mappedBy = ...) exposes the children from the parent and does not write anything itself. Keep both sides in sync with helper methods, make @ManyToOne lazy, cascade only from parent to child, and use fetch joins or entity graphs to avoid N+1 queries.