Hibernate: Pagination with join fetch loads everything into memory

A join multiplies rows, so Hibernate cannot page in SQL and warns before doing it in Java instead.

Code
// Dangerous:
@Query("select o from Order o join fetch o.items")
Page<Order> paged(Pageable pageable);
Output
WARN HHH90003004: firstResult/maxResults specified with collection fetch;
applying in memory

Fix: page the ids first, then fetch the items for that page.
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