SteelReed

Search

Search posts, questions…

JPA & HibernateQuestion 13 / 24

Junior

How does pagination work in Spring Data? How is Page different from Slice?

Short answer

Spring Data takes a Pageable (page counted from 0, size, sort) and builds LIMIT/OFFSET. Page adds a COUNT for the total; Slice only knows if a next page exists.

PageSlicepage 2, size 10LIMIT 10 OFFSET 20Return type?extra COUNTknows total pagesfetch size + 1 rowsonly hasNext

3 points to say:

  1. Spring Data pages are counted from 0.
  2. Cap the page size and whitelist sort columns.
  3. Always end the sort with a unique key such as id.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.