Spring Data JPA Pagination Example with Using Pageable Parameter

Posted By : Saurabh Shukla | 29-Aug-2019

  Creating PageRequest object for pagination

Since Spring Data 2.0, creating a new PageRequest object using constructors is deprecated now. Instead, use of constructor of the PageRequest, we can use of() static method as a factory method for creating PageRequest Object. In a PageRequest object, pages are zero-indexed, thus providing 0 for a page will return the first page.
 

Several overloaded versions of the of() methods provided to create  PageRequest object. This object fulfills the pagination conditions as following:
 

Condition-I 

If we want to create a PageRequest object to specify a query results belonging to a single page. Then we have to create the PageRequest object as the following code 

 

 

public static PageRequest of(int page, int size) {

     //implementation

}

//In service class get the query results referring to the first page when page size is 10.

PageRequest.of(0, 10)

Condition-II

 if you want to create a PageRequest object to specify the results belonging to a single page but the query results are sorted by using the value of a single field. Then we have to create the PageRequest object as the following code

 

 

public static PageRequest of(int page, int size, Sort sort) {

     //implementation

}

//In service class get the query results referring to the first page when page size is 10.

//Query results are sorted in descending order by using a price field.

PageRequest.of(0, 10 Sort.Direction.DESC, "price")

Condition-III

In this requirement, if you want to create a PageRequest object to specify the query results belonging to a single page but the query results are sorted by using multiple fields and sort order of different fields is the same. Let’s see the following code 

 

 

Public static PageRequest of(int page, int size, Direction direction, String... properties) {

     //implementation

}

//In service class get the query results referring to the first page when page size is 10.

//Query results are sorted in descending order by using oderId and orderDesc fields.

PageRequest.of(0, 10 Sort.Direction.DESC, "orderId", "orderDesc")

Condition-IV

This requirement is very suitable when we want to create a PageRequest object to specify the query results belonging to a single page when the query results are sorted by using multiple fields and the sort order of different fields is not same. Let’s see the following code snippet to create PageRequest object:

 

 

public static PageRequest of(int page, int size, Direction direction, String... properties) {

     //implementation

}

/In service class get the query results referring to the first page when page size is 10

//Query results are sorted in descending order by using orderId field and ascending order by using orderDesc field.

Sort sort = Sort.by(Sort.Direction.DESC, "orderId").and(Sort.by(Sort.Direction.ASC, "orderDesc"))

PageRequest.of(0, 10, sort)

About Author

Author Image
Saurabh Shukla

Saurabh Shukla is a software developer having key skills in Grails and J2EE. His hobbies are playing chess and learning new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..