@ManyToOne is the Jakarta Persistence annotation that maps the "many" side of a relationship: many rows of one table reference a single row of another. Place it on a field of the entity that holds the foreign key, for example Employee.department, and JPA maps it to a foreign key column and loads the referenced entity for you.
It is the most common association in JPA, and the one most often responsible for hidden performance problems. The default fetch type is EAGER, which is usually the wrong choice, so you normally set fetch = FetchType.LAZY yourself.
In the database, a many-to-one relationship is a foreign key. Many employee rows point to one department row through a column such as department_id. In Java, the entity on the many side holds a reference to the single parent:
The generated table has a department_id column with a foreign key to department. Entities are regular classes with a no-argument constructor, not records. Imports use jakarta.persistence in Spring Boot 3 and later (javax.persistence before).
| Attribute | Meaning | Default |
|---|---|---|
fetch | LAZY or EAGER loading of the parent | EAGER |
optional | Whether the association may be null | true |
cascade | Which operations propagate to the parent | none |
targetEntity | Target type when it cannot be inferred | inferred |
@JoinColumn names the foreign key column, and nullable = false adds a NOT NULL constraint. Setting optional = false tells the provider the value is never null, which lets it skip some null checks and use inner joins. Keep optional and nullable consistent with each other.
With EAGER, loading an Employee always loads its Department. For a single entity that is a join. For a list of employees it can become the N+1 query problem: one query for the employees and one extra query per distinct department. Declaring fetch = FetchType.LAZY loads a proxy and runs the query only when you access the department.
When you do need the parent in a particular use case, load it explicitly in the query:
join fetch loads employees and departments in one SQL statement. Spring Data also supports @EntityGraph(attributePaths = "department") on repository methods. Accessing a lazy association after the session closed throws LazyInitializationException, so fetch what you need inside the transaction, or map to a DTO there.
Often you also want department.getEmployees(). Add @OneToMany(mappedBy = ...) on the parent. The @ManyToOne side owns the foreign key, and mappedBy points to its field name:
Keep both sides in sync with a helper method like addEmployee, since JPA writes only the owning side (@ManyToOne) to the database. A bidirectional mapping is optional. A unidirectional @ManyToOne is simpler, and you can query employees by department with a repository method such as findByDepartmentId.
Cascade usually belongs on the parent's @OneToMany, not on @ManyToOne. Placing CascadeType.REMOVE or ALL on @ManyToOne would delete the department when one employee is deleted, which is almost never intended. If you delete a parent that still has children, the database foreign key rejects it unless you remove the children first or cascade from the parent.
The @ManyToOne side always owns it, because it holds the foreign key. In a bidirectional mapping, @OneToMany uses mappedBy to refer back to it.
Eager. Set fetch = FetchType.LAZY explicitly in almost all cases and fetch the parent with join fetch or an entity graph when needed.
No. Without it, the provider derives a column name such as department_id. Adding it makes the name and nullability explicit, which is better for schema stability.
Use lazy loading and fetch joins, entity graphs or DTO projections for the queries that need the parent, and check the generated SQL in tests.
@ManyToOne maps a foreign key to a parent entity. Declare it lazy with @JoinColumn, mark it non-optional when the parent is required, load the parent explicitly where you need it, and add @OneToMany(mappedBy = ...) only when you actually need to navigate from parent to children.