5

I'm wondering if there is simple way (or at least plan to add the feature) how to return Slice without count query while passing specification to JpaSpecificationExecutor .findAll(Specification spec, Pageable pageable)

So I want to do something like this:

Slice<MessageViewEntity> messageViewEntities =
                messageViewRepository.findAll(
                        messageViewRepository.withSearchSpecifications(language, categoryId, messageKey, longText),
                        new PageRequest(page, size)
                );

Where count query won't be executed.

I've found this question which is almost 2 years old but in current release 10.1.2 I don't see method/way how to do this.

Thanks

Community
  • 1
  • 1
bilak
  • 4,526
  • 3
  • 35
  • 75

1 Answers1

1

It can be done. Look here: https://gist.github.com/tcollins/0ebd1dfa78028ecdef0b

Basically you need to:

  • extend SimpleJpaRepository so you have access to protected getQuery(Specification ..) methof returning TypedQuery
  • then on typed query you can set offset & max results (size of slice + 1)
  • then you can execute TypedQuery .getResultList() & create Slice result
Tomasz Modelski
  • 412
  • 6
  • 10