<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a" -->

---
title: Spring Data JPA : Pagination While Loading Child Collection
description: A walkthrough explains a common Hibernate/Spring Data JPA pitfall where paginating an entity while also fetch-joining a child collection (e.g., posts with...
canonical: https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Spring Data JPA : Pagination While Loading Child Collection | daily.dev
og:description: A walkthrough explains a common Hibernate/Spring Data JPA pitfall where paginating an entity while also fetch-joining a child collection (e.g., posts with...
og:url: https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a
og:image: https://api.daily.dev/og/posts/ZWrdnLU8a.png
og:image:alt: Spring Data JPA : Pagination While Loading Child Collection
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Spring Data JPA : Pagination While Loading Child Collection

**[SivaLabs \(YouTube\)](https://daily.dev/sources/sivalabs-youtube)** · 16 min read · 0 upvotes · 0 comments

## Summary

A walkthrough explains a common Hibernate/Spring Data JPA pitfall where paginating an entity while also fetch-joining a child collection (e.g., posts with comments) causes Hibernate to load the entire dataset into memory before applying pagination, triggering a 'firstResult/maxResults specified with collection fetch' warning. The fix involves two steps: setting the hibernate.query.fail_on_pagination_over_collection_fetch property to true so the app fails fast during development instead of silently loading everything, and restructuring the query into two DB calls — first fetching paginated post IDs only, then fetching those specific posts with their comments via an IN clause, avoiding the in-memory pagination problem entirely.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=2Y0eCAEbAV4>

## Questions this post answers

### Why does Hibernate log 'firstResult/maxResults specified with collection fetch; applying in memory' when paginating with a join fetch?

This happens because pagination cannot be applied at the SQL level when a collection (like comments) is fetch-joined to the parent entity, since the join produces multiple rows per parent. Hibernate instead loads the entire result set into memory, aggregates the child rows into their parents, and then manually applies the page limit, which can exhaust memory on large datasets.

_Developers debugging this exact Hibernate warning can find deep dives like this one through daily.dev._

### How do I set Hibernate to fail instead of silently doing in-memory pagination over a collection fetch?

Set the property spring.data.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch to true in a Spring Boot application's configuration. With this enabled, Hibernate throws an exception during development whenever it would otherwise load the full dataset into memory to paginate a collection fetch, surfacing the problem before it reaches production.

_Anyone hardening Spring Boot data access configs can track fixes like this via daily.dev._

### What is the recommended pattern for paginating an entity while also loading its child collection in Spring Data JPA?

Split the operation into two database calls: first query for just the paginated set of parent entity IDs (e.g., post IDs) using a pageable, then run a second query that fetch-joins the child collection only for that specific set of IDs using an IN clause. This avoids pagination ever combining with a collection fetch in a single query, preventing the in-memory pagination problem.

_Teams designing paginated APIs with nested collections can compare implementation patterns on daily.dev._

## Similar posts on daily.dev

- [Hibernate Turned 11,000 Records Into 50 Million Rows](https://daily.dev/posts/hibernate-turned-11-000-records-into-50-million-rows-gsexbezep) · Code Like A Girl · 38 upvotes · 2 comments
- [Offset and Keyset Pagination with Spring Data JPA](https://daily.dev/posts/offset-and-keyset-pagination-with-spring-data-jpa-nya5hmupa) · Jakarta EE · 0 upvotes · 0 comments
- [We Fixed N\+1 With Eager Loading. Memory Usage Tripled.](https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth) · Medium · 54 upvotes · 7 comments

---

Tags: [#java](https://daily.dev/tags/java), [#spring](https://daily.dev/tags/spring), [#spring-boot](https://daily.dev/tags/spring-boot)

[View this post on daily.dev](https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Spring Data JPA : Pagination While Loading Child Collection","url":"https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a"},"datePublished":"2026-09-27T07:26:39.991Z","dateModified":"2026-09-27T07:53:30.852Z","description":"A walkthrough explains a common Hibernate/Spring Data JPA pitfall where paginating an entity while also fetch-joining a child collection (e.g., posts with...","image":"https://i.ytimg.com/vi/2Y0eCAEbAV4/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/2Y0eCAEbAV4/sddefault.jpg","isAccessibleForFree":true,"articleSection":"SivaLabs (YouTube)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SivaLabs (YouTube)","logo":"https://media.daily.dev/image/upload/s--8ATVo4an--/f_auto,q_auto/v1790493987/logos/sivalabs-youtube?_a=BAMAMicg0","url":"https://daily.dev/sources/sivalabs-youtube"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,spring,spring-boot","timeRequired":"PT16M","video":{"@type":"VideoObject","name":"Spring Data JPA : Pagination While Loading Child Collection","description":"A walkthrough explains a common Hibernate/Spring Data JPA pitfall where paginating an entity while also fetch-joining a child collection (e.g., posts with...","thumbnailUrl":"https://i.ytimg.com/vi/2Y0eCAEbAV4/sddefault.jpg","uploadDate":"2026-09-27T07:26:39.991Z","duration":"PT16M","url":"https://api.daily.dev/r/ZWrdnLU8a","embedUrl":"https://www.youtube.com/embed/2Y0eCAEbAV4"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SivaLabs (YouTube)","item":"https://daily.dev/sources/sivalabs-youtube"},{"@type":"ListItem","position":3,"name":"Spring Data JPA : Pagination While Loading Child Collection"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/spring-data-jpa-pagination-while-loading-child-collection-zwrdnlu8a#faq","mainEntity":[{"@type":"Question","name":"Why does Hibernate log 'firstResult/maxResults specified with collection fetch; applying in memory' when paginating with a join fetch?","acceptedAnswer":{"@type":"Answer","text":"This happens because pagination cannot be applied at the SQL level when a collection (like comments) is fetch-joined to the parent entity, since the join produces multiple rows per parent. Hibernate instead loads the entire result set into memory, aggregates the child rows into their parents, and then manually applies the page limit, which can exhaust memory on large datasets. Developers debugging this exact Hibernate warning can find deep dives like this one through daily.dev."}},{"@type":"Question","name":"How do I set Hibernate to fail instead of silently doing in-memory pagination over a collection fetch?","acceptedAnswer":{"@type":"Answer","text":"Set the property spring.data.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch to true in a Spring Boot application's configuration. With this enabled, Hibernate throws an exception during development whenever it would otherwise load the full dataset into memory to paginate a collection fetch, surfacing the problem before it reaches production. Anyone hardening Spring Boot data access configs can track fixes like this via daily.dev."}},{"@type":"Question","name":"What is the recommended pattern for paginating an entity while also loading its child collection in Spring Data JPA?","acceptedAnswer":{"@type":"Answer","text":"Split the operation into two database calls: first query for just the paginated set of parent entity IDs (e.g., post IDs) using a pageable, then run a second query that fetch-joins the child collection only for that specific set of IDs using an IN clause. This avoids pagination ever combining with a collection fetch in a single query, preventing the in-memory pagination problem. Teams designing paginated APIs with nested collections can compare implementation patterns on daily.dev."}}]}
```

