0Pricing
Java Academy · Lesson

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

  1. Entity Mapping with JPA Annotations
  2. Spring Data Repositories and Query Methods
  3. One-to-Many and Many-to-Many Relationships
  4. Pagination, Sorting, and Projections
← Back to Java Academy