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

2

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️