#Filter with pagination in Spring

5 messages · Page 1 of 1 (latest)

humble copper
#

Hi everyone. I'm working on adding a filter with pagination to a query in my application. My current issue is that I can only filter objects already loaded into memory, not directly within the database query. Also, the Page method seems to lack a built-in filtering function. Any suggestions?

gloomy cragBOT
#

This post has been reserved for your question.

Hey @humble copper! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dull parrot
#

spring's pagination is different from completely generic relational database condition filtering, since spring's pages can be used in more situations than just relational databases. Therefore, you should use JPA's criteria API to dynamically build a query according to your user's filters. See here: https://docs.spring.io/spring-data/jpa/reference/jpa/specifications.html

#

essentially, you can make your repository extend JpaSpecificationExecutor<EntityType> and then you get access to Page<EntityType> findAll(Specification<EntityType>, Pageable);