One-to-Many and Many-to-Many Relationships
Map @OneToMany, @ManyToMany, and @JoinTable with cascade types and fetch strategies.
Relationship Types in JPA
JPA supports four relationship types: @OneToOne, @OneToMany, @ManyToOne, @ManyToMany. Each maps to foreign-key or join-table constraints.
@ManyToOne: The Owning Side
A many-to-one relationship (e.g., many Orders belong to one User) is mapped with @ManyToOne and a @JoinColumn for the foreign key.
@Entity
public class Order {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
}All lessons in this course
- Entity Mapping with JPA Annotations
- Spring Data Repositories and Query Methods
- One-to-Many and Many-to-Many Relationships
- Pagination, Sorting, and Projections