DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for The Pageable object of Spring Data JPA implements paging of the queried list
Keith
Keith

Posted on • Originally published at Medium

The Pageable object of Spring Data JPA implements paging of the queried list

Image description

Pageable object

Using the Pageable object of Spring Data JPA can perform database query and paging. This implementation has been introduced in many blogs, so I won’t list it anymore. You can refer to the link:
https://www.tianmaying.com/tutorial/spring-jpa-page-sort
But there is a situation that pageable cannot paginate the list results of the queried data, which is almost unavoidable in actual development. For many complex businesses, for simplifying development or considering practical reasons, it is impossible to achieve the requirements through a sql query , it will definitely filter the queried list data, and the paging function of the pageable object is invalid at this time, refer to the code.

List<Impianto> impiantos = myService.findMyMethod(); // returned 30 objects 
    Page<Impianto> pageImpianto = new PageImpl<Impianto>(impiantos, pageable, impiantos.size());
Enter fullscreen mode Exit fullscreen mode

This implementation cannot achieve paging.

According to the spring data jpa author:

Spring Data repositories support pagination on query methods by simply declaring a parameter of type Pageable to make sure they’re only reading the data necessary for the requested Page.
The pageable object is only a basic implementation of paging, and cannot achieve paging of the queried list results. In my opinion, this implementation has almost no practical engineering significance. It is recommended not to use pageable objects for paging in actual project development. If you have different opinions, you can leave a message for discussion.
For this reason, if the pageable object wants to achieve paging, it can only process the data manually. The sample code is as follows:

if (pageable.getOffset() > ucShopCourseBizPojoList.size()) {
    long total = 0L;
    PageImpl<UcShopCourseBizPojo> emptyPage = new PageImpl<>(Lists.newArrayList(), pageable, total);
    resultDo.setResult(emptyPage);
    return resultDo;
}

if (pageable.getOffset() <= ucShopCourseBizPojoList.size() && pageable.getOffset() + pageable.getPageSize() > ucShopCourseBizPojoList.size()) {
    List<UcShopCourseBizPojo> bizPojos = ucShopCourseBizPojoList.subList(pageable.getOffset(), ucShopCourseBizPojoList.size());
    PageImpl<UcShopCourseBizPojo> pPage = new PageImpl<>(bizPojos, pageable, ucShopCourseBizPojoList.size());
    resultDo.setResult(pPage);
    return resultDo;
}

List<UcShopCourseBizPojo> ucShopCourseBizPojos = ucShopCourseBizPojoList.subList(pageable.getOffset(), pageable.getOffset() + pageable.getPageSize());

PageImpl<UcShopCourseBizPojo> pPage = new PageImpl<>(ucShopCourseBizPojos, pageable, ucShopCourseBizPojoList.size());
Enter fullscreen mode Exit fullscreen mode

ucShopCourseBizPojoList is the list to be paginated.

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.