Egor Shurov
Egor Shurov

Reputation: 21

How make multi fields filter with JpaRepository and keep pagination

I want to make multi fields filter with JpaRepository, but I need to keep pagination(Page).

I tried the following:

Page<Group> getAllByOrganizationAndIdNotInAndLocation_Id(Organization organization, Collection<Long> id, Long location_id, Pageable pageable);

However, in some cases, I do not receive all the expect variables and the query does not work as I intended

Upvotes: 2

Views: 3015

Answers (2)

torshid
torshid

Reputation: 167

You may just use this library in combination with Spring's Pageable: https://github.com/turkraft/spring-filter

It will let you run search queries such as:

/search?filter= average(ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty

Spring's Pageable will let you paginate with &page=34&size=20

Upvotes: 1

Robert Niestroj
Robert Niestroj

Reputation: 16141

When variables are optional you need to handle their absence. You need to dynamically create your query. In Spring Data JPA you have three options:

  • Criteria API
  • QueryDSL
  • Query By Example

You can read more on that here: Dynamic spring data jpa repository query with arbitrary AND clauses

Upvotes: 2

Related Questions