Skip to content
Ameen Abozaid
Planned lessonJNR-032-L02

Fetch joins and entity graphs

  • Lesson 2 of 7
  • Junior
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 fetch in 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

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.