Concept
@OneToMany, @ManyToOne and @ManyToMany map foreign keys to object references. How they are fetched, lazily or eagerly, decides how many queries a page load makes.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Relationships doing its job.
@Entity
public class Order {
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<LineItem> items = new ArrayList<>();
}
@Entity
public class LineItem {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Order order;
}
// Loading 50 orders with their items in one query:
@Query("select o from Order o join fetch o.items where o.status = :status")
List<Order> findWithItems(OrderStatus status);The N+1 query problem: load a list of orders, then touch order.getItems() in a loop, and each one fires its own query. Fetch the relationship in the query that loads the list.
Your roadmap puts these before Relationships, unless you already know them.
Once a session shows you understand Relationships, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.