Pagination in spring data jpa

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

In previous articles we have seen how to sort entities. Sometimes we come across requirement where there is huge resultset and it need not be fetched all at once. We have to fetch small sections of big resultset. This article will deal with pagination in spring data jpa.

Pagination in Spring Data JPA:

We are going to use Pageable interface and its implementation PageRequest for pagination in spring data jpa.

We are going to use same example as we used in last article.

Employee.java:

EmployeeRepository.java:

Note here that we are passing a Pageable type to method that returns employees with salary greater than passed in parameter.

Let us try it out with code.

You can note here that  Pageable pageable = new PageRequest(0, 5, sort); initialises a PageRequest which starts from zeroth result and page size is 5. A Sort object is passed to it which sorts object according to is property as explained in previous article.

above program outputs,

Employee 2
Employee 3
Employee 4
Employee 5
Employee 6

I hope the article helped understand pagination in spring data jpa.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *