@OneToMany, @ManyToOne and @ManyToMany are the JPA annotations that map relationships between entities to foreign keys and join tables. @ManyToOne is the "many" side holding a foreign key to one parent, @OneToMany is the collection on the parent, and @ManyToMany links two entities through a join table.
The examples below use the jakarta.persistence package (Jakarta Persistence 3.x, Hibernate 6, Spring Boot 3). On older stacks the same annotations are in javax.persistence.
| Annotation | Meaning | Where the data lives | Default fetch |
|---|---|---|---|
@ManyToOne | Many children point to one parent | Foreign key column on the child table | EAGER |
@OneToMany | One parent has many children | Foreign key on the child, or a join table | LAZY |
@ManyToMany | Many on both sides | A separate join table | LAZY |
The side that holds the foreign key (or defines the join table) is the owning side. The other side uses mappedBy and is only a read-friendly view of the same data. JPA writes to the database based on the owning side only.
@ManyToOne is the simplest and most efficient relationship. The child has a column such as department_id.
Because @ManyToOne is eager by default, set fetch = FetchType.LAZY explicitly unless you really need the parent every time. Use optional = false for a mandatory relationship, which also lets the schema use NOT NULL.
The inverse side is a collection that points back with mappedBy.
The helper methods keep both sides in sync. Without mappedBy, a @OneToMany defaults to a join table, which is rarely what you want.
A many-to-many relationship needs a join table with two foreign keys. One side owns it with @JoinTable.
Use a Set rather than a List. With a List (a "bag"), Hibernate may delete and re-insert all join rows when one element changes.
If the relationship has its own data, such as an enrolment date or grade, do not use @ManyToMany. Model the join table as an entity (Enrollment) with two @ManyToOne associations.
PERSIST and MERGE on @ManyToMany only when you need them. Never use CascadeType.REMOVE or ALL on @ManyToMany, because deleting one student would delete courses shared with others.ALL with orphanRemoval = true is reasonable on @OneToMany when the child belongs only to that parent.join fetch or @EntityGraph to avoid the N+1 select problem.LazyInitializationException. Load data in the service layer or return DTOs.NULL even though the collection looks filled.@Data, @ToString or @EqualsAndHashCode on entities with bidirectional links, which causes infinite recursion.@ManyToOne eager, which causes extra joins or queries on every load.The side with the foreign key column. For @OneToMany/@ManyToOne it is the @ManyToOne side; for @ManyToMany it is the side that declares @JoinTable.
Yes, as a unidirectional mapping, but it uses a join table unless you add @JoinColumn, and the SQL is less efficient than a bidirectional mapping.
Use Set for @ManyToMany. For @OneToMany either works, but avoid fetching several List collections in one query.
Create an association entity for the join table with two @ManyToOne fields and the extra attributes.
@ManyToOne holds the foreign key, @OneToMany is its collection view with mappedBy, and @ManyToMany uses a join table that is best replaced by an entity when it carries data. Keep associations lazy, keep both sides in sync with helper methods, and cascade deliberately.