Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Result limit using JPA Specification

Is there possibility to use limit or setMaxResults using Spring-Data-JPA Specification? I can use it via Criteria or JPQL, but I need only via specifications. Specifications have CriteriaQuery, not a Query. So, there is no setMaxResults method.

like image 835
Squeez Avatar asked Mar 15 '17 08:03

Squeez


1 Answers

I think you only have two options:

Use Pageable with your Specification:

Pageable limit = new PageRequest(0,10);
repo.findAll(spec, limit);

or do a workaround like this one: https://gist.github.com/tcollins/0ebd1dfa78028ecdef0b. All the credits to @tcollins

like image 52
Cleto Gadelha Avatar answered Sep 18 '22 21:09

Cleto Gadelha