davidddp
davidddp

Reputation: 591

Spring Boot repository with Example, Specification and Pageable together

I need to make a complex pagitable filter in the repository. I have succeeded, using Example and Pageable extending from:

extends JpaRepository <Card, Long>

Which in turn extends from:

extends PagingAndSortingRepository <T, ID>, QueryByExampleExecutor <T>

Right now the call is this:

Page<Card> findByCreatedDateBetween (Example<Card> exampleCard, Pageable pageable);

PROBLEM: I need to search by a date, which must be between two given dates. In Example<Card> it is not possible to indicate a range.

I have thought to do it with Specification, but I don't know how to make a call with the 3 parameters:

Page<Card> findByCreatedDateBetween (Specification<Card> cardSpecification, Example<Card> exampleOffer, Pageable pageable);

The same is there another solution?

Upvotes: 1

Views: 599

Answers (1)

Dario Seidl
Dario Seidl

Reputation: 4640

You cannot use both an Example and a Specification in one query, since QueryByExampleExecutor doesn't provide such a method.

You can also look at SimpleJpaRepository if you're interested in the implementation of QueryByExampleExecutor. Internally the implementation creates a Specification from the Example, using QueryByExamplePredicateBuilder. You may be able to use that to build your own Specification from an Example using that and then combine that Specification with another one.

The ExampleSpecification in SimpleJpaRepository is private, so you cannot use that, but you could copy it, and then do something like

findAll(ExampleSpecification(yourExample, escapeCharacter).and(yourDateRangeSpecification), pageable);

Upvotes: 1

Related Questions