Hibernate: @EntityGraph declares what to fetch, per query

Same effect as join fetch, but it keeps the fetch plan out of the JPQL and works with derived methods.

Code
@EntityGraph(attributePaths = {"items", "customer"})
@Query("select o from Order o where o.status = :status")
List<Order> withItems(@Param("status") Status status);

// Also works on a derived method:
@EntityGraph(attributePaths = "items")
List<Order> findByCustomerId(Long customerId);
Output
One query with two left joins,
and the same repository method still returns entities rather than a DTO.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11