The @ManyToOne annotation maps a many-to-one relationship between two JPA entities: many rows of the child table point to one row of the parent table through a foreign key column. It is the owning side of the relationship in almost every real model, for example many Order rows referencing one Customer.
In current Spring Boot 3 and later and Hibernate 6 and later, the annotation lives in the jakarta.persistence package. Older code that imports javax.persistence only works on Java EE and Spring Boot 2 stacks.
When you place @ManyToOne on a field, JPA adds a foreign key column to the table of the entity that declares it. That entity owns the relationship, because the foreign key lives in its table.
This produces an orders table with a non-null customer_id column and a foreign key constraint to customer(id).
| Attribute | Default | Meaning |
|---|---|---|
fetch | FetchType.EAGER | When the parent is loaded. Use LAZY in almost all cases |
optional | true | Whether the association may be null. false lets the provider assume it is always set |
cascade | none | Which persistence operations propagate to the parent |
targetEntity | inferred | Only needed when the field type is not the entity class |
The column itself is configured with @JoinColumn: name sets the foreign key column, nullable adds or removes the NOT NULL constraint, referencedColumnName points at a non-primary-key column, and foreignKey customises the constraint.
If you omit @JoinColumn, JPA names the column from the field name and the parent's primary key, such as customer_id.
The JPA specification defaults @ManyToOne to eager loading, which means every query that loads an Order also loads its Customer. That is the main source of the N+1 select problem. Set fetch = FetchType.LAZY explicitly and load the parent only when you need it.
When you do need the parent for a list of children, fetch it in the query instead of relying on eager loading:
Spring Data JPA also supports @EntityGraph(attributePaths = "customer") on a repository method for the same effect without writing JPQL.
@ManyToOne works alone as a unidirectional mapping. If you also need to navigate from parent to children, add a @OneToMany on the parent with mappedBy, which points to the field name of the @ManyToOne side:
Only the @ManyToOne side writes the foreign key, so the helper methods must keep both sides in sync. Without mappedBy, JPA would treat the @OneToMany as a separate owning mapping and create an extra join table.
Be careful with cascade on @ManyToOne. Putting CascadeType.REMOVE or ALL on the child side would delete the parent, and with it every other child, when one child is deleted. Cascading normally belongs on the @OneToMany side, where the parent owns the lifecycle of its children. A @ManyToOne rarely needs more than PERSIST or MERGE, and often needs no cascade at all.
EAGER fetch and triggering N+1 queries.@Data or @EqualsAndHashCode from Lombok on entities, which can cause infinite recursion in toString and hashCode across a bidirectional link.optional = true for a column that is actually NOT NULL in the schema.Yes. It always owns the relationship because the foreign key column is in its table. The @OneToMany side uses mappedBy and is the inverse side.
No, JPA generates a default name, but declaring it makes the schema explicit and lets you control nullability and the constraint.
Lazy in nearly all cases. Eager is the default but forces the parent to load every time the child is loaded.
Yes, use @JoinColumn(referencedColumnName = "code"). The referenced column should be unique.
@ManyToOne declares the child side of a many-to-one relationship and creates the foreign key column. Set fetch = LAZY, declare the column with @JoinColumn, keep bidirectional links in sync through helper methods and use join fetch or entity graphs when you need the parent in a query.