In short
Real data is connected: a customer has many orders, an order has many items. Getting JPA relationships right is the difference between one quick query and a slow page that fires hundreds of them.
JPA models links between tables with @OneToOne, @OneToMany, @ManyToOne and @ManyToMany. Fetch types, cascades and the owning side decide what SQL runs, and lazy loading can cause the N+1 query problem.

Think of it like this
Class register and roll numbers
In a school, each student's form says which class they belong to (the student holds the link, like @ManyToOne). The class teacher's register lists all students (@OneToMany), but that list is built from the students' forms, not stored twice. The side that actually writes the link is the owning side.
Watch it happen
Customer, orders and items
1/4Load the customer. With LAZY orders, only one SELECT runs.
The big ideas
What you need to know
@ManyToOne owns the key
The many side holds the foreign key column with @JoinColumn. It is usually the owning side.
mappedBy
@OneToMany(mappedBy = "customer") says the other side owns the link, so no extra join table is created.
Make it LAZY
@ManyToOne is EAGER by default. Set fetch = FetchType.LAZY and fetch what you need per query.
N+1 problem
One query for the parents plus one per parent for children. Fix it with JOIN FETCH or entity graphs.
See it in code
Walk through the code
1import jakarta.persistence.*;2import java.util.ArrayList;3import java.util.List;4 5@Entity6@Table(name = "customers")7public class Customer {8 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)9 private Long id;10 private String name;11 12 @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)13 private List<PurchaseOrder> orders = new ArrayList<>();14 15 public void addOrder(PurchaseOrder order) {16 orders.add(order);17 order.setCustomer(this); // keep both sides in sync18 }19}20 21@Entity22@Table(name = "orders")23public class PurchaseOrder {24 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)25 private Long id;26 27 @ManyToOne(fetch = FetchType.LAZY)28 @JoinColumn(name = "customer_id", nullable = false)29 private Customer customer;30 31 public void setCustomer(Customer customer) { this.customer = customer; }32}33 34// Load customers with their orders in one query35List<Customer> list = em.createQuery(36 "SELECT DISTINCT c FROM Customer c LEFT JOIN FETCH c.orders", Customer.class)37 .getResultList();1/4The inverse side. mappedBy points at the field on PurchaseOrder that owns the link.
Each class goes in its own file. Order is a reserved word in SQL, so the entity is PurchaseOrder mapped to an orders table.
Save yourself the pain
Common mistakes
Oops
Setting only customer.getOrders().add(order) and expecting the foreign key to be saved.
Do this instead
Only the owning side is written. Set order.setCustomer(customer), ideally through a helper that sets both.
Oops
Leaving @ManyToOne as the default EAGER everywhere.
Do this instead
Make it LAZY and fetch related data explicitly per use case.
Oops
Using CascadeType.ALL on a @ManyToOne to a shared parent.
Do this instead
Deleting one order could then try to delete the customer. Cascade from parent to children, not the other way.
Your mission
Spot and fix N+1
Quick check
In a bidirectional Customer and PurchaseOrder relationship, which side should have mappedBy?
Go deeper
- 1Map a Department with many Employees using @OneToMany and @ManyToOne.
- 2Turn on SQL logging, load departments with their employees and spot the N+1 queries.
- 3Fix the N+1 problem with a JOIN FETCH query or an entity graph.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.