Skip to main content

Pagination, Sorting, Aggregates, Joins, Subqueries, Projections and DTO Projections

Learn Pagination, Sorting, Aggregates, Joins, Subqueries, Projections and DTO Projections (Phase 5 · Hibernate Querying (Core), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Pagination and sorting • Offset pagination : query.setFirstResult((page - 1) * size).setMaxResults(size) adds LIMIT/OFFSET (or FETCH FIRST ). Always combine with a deterministic order by (including a unique tiebreaker such as the id) or pages overlap. • Pagination with collection fetch joins is dangerous : Hibernate cannot limit in SQL and logs HHH000104: firstResult/maxResults specified with collection fetch; applying in memory (loads everything!). Paginate the root ids first , then fetch children (see performance section) or use batch fetching. • Keyset (seek) pagination ( where (created_at,...

More lessons in Phase 5 · Hibernate Querying (Core)