Fetch joins and entity graphs
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
load related data in one query.
Planned topics
join fetchin JPQL loads the relation together with the parent.@EntityGraph(attributePaths = "lines")on a repository method does the same without writing JPQL.- A projection that selects only needed columns can avoid loading entities at all.
- Fetch what the use case needs, not everything.
- Verify with the log or the query counter.
Draft outline details
Demonstration
@Query("select distinct o from Order o join fetch o.lines where o.customer.id = :cid")
List<Order> findWithLines(@Param("cid") Long cid);
@EntityGraph(attributePaths = {"lines", "customer"})
List<Order> findByStatus(OrderStatus status);Check your understanding
Name two ways to fix N+1 besides projections.
Show answer
Fetch join and entity graph.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.