The @ManyToOne annotation maps a many-to-one association between two entities: many rows of one table point to a single row of another. It lives on the child entity (the "many" side), creates a foreign key column, and makes that side the owner of the relationship in Jakarta Persistence (JPA).
For example, many Book rows can reference one Author. The books table holds an author_id column, and @ManyToOne on Book.author tells the provider (Hibernate, EclipseLink) to read and write it. The examples below use the jakarta.persistence package (Jakarta Persistence 3.x, used by Spring Boot 3 and later); older code uses javax.persistence with identical annotations.
@ManyToOne is placed on a single-valued field that references another entity. The provider stores the referenced entity's primary key in a foreign key column of the owning table.
The generated schema looks roughly like this:
Without @JoinColumn, the column name defaults to the field name, an underscore, and the referenced primary key name (author_id here).
| Attribute | Default | Purpose |
|---|---|---|
fetch | FetchType.EAGER | Whether the target is loaded together with the owner or on first access |
optional | true | If false, the association must never be null (the provider can enforce it and generate NOT NULL) |
cascade | none | Which persistence operations propagate to the target |
targetEntity | inferred from the field type | Rarely needed; only for raw or untyped references |
@JoinColumn complements it with name, nullable, referencedColumnName, unique and foreignKey to control the column and constraint.
The JPA specification makes @ManyToOne eager by default. Loading a list of 100 books then triggers extra queries or joins for every author, even when you never read them. In almost every application you should declare fetch = FetchType.LAZY and load associations on purpose.
With a lazy association, Hibernate injects a proxy, and the real row is read on first access. That access must happen inside an open persistence context (for example a transaction), otherwise you get a LazyInitializationException.
A many-to-one association is often paired with @OneToMany on the other side so you can navigate from an author to its books. The @ManyToOne side stays the owner, and the @OneToMany side points back with mappedBy.
Only the owning side (Book.author) decides what is written to the foreign key. If you add a book to author.getBooks() without calling book.setAuthor(author), nothing is persisted. The helper methods above keep both sides in sync.
Because each Book has a lazy Author, iterating over books and reading getAuthor().getName() runs one extra query per distinct author. Fix it by fetching the association in the query that needs it.
In Spring Data JPA you can get the same result with @EntityGraph(attributePaths = "author") on a repository method. For read-only screens, selecting a DTO projection avoids loading entities altogether.
CascadeType.REMOVE or CascadeType.ALL on @ManyToOne. Deleting one book would delete the author, and with it every other book that references that author.CascadeType.PERSIST or MERGE on the many side is occasionally useful, but usually it is clearer to persist the parent explicitly.@ManyToOne on a collection field. Use @OneToMany or @ManyToMany instead.optional = false and nullable = false in agreement, so the entity model and the schema describe the same rule.They describe the same relationship from opposite sides. @ManyToOne is on the entity holding the foreign key and is the owning side. @OneToMany(mappedBy = "...") is the inverse side that exposes a collection.
Eager. The specification requires FetchType.EAGER as the default for @ManyToOne and @OneToOne, so set fetch = FetchType.LAZY explicitly.
No, the provider applies a default name. Add @JoinColumn when you need a specific column name, a NOT NULL constraint or a custom foreign key name.
Yes, unless you set optional = false. A null reference is stored as a null foreign key.
@ManyToOne maps the "many" side of a relationship to a foreign key column and is the owning side of the association. Declare it with fetch = FetchType.LAZY, mark required relationships with optional = false, keep bidirectional links in sync with helper methods, avoid cascading removal, and use join fetch or entity graphs to prevent N+1 queries.