Spring JPA data select Individual values ​​with the ability to use Pageable

I am using Spring Data JPA in the project. I have the following Entity Bean "TABLE". I created my own repository

TableRepository extends JpaRepository<Table, Long>, JpaSpecificationExecutor<TABLE>. 

I also created a specification for selecting filtered data. I have a Bean with elements to select and group them into a single predicate list .in () (something like "SELECT * FROM WHERE ID in (...) and VALUE in (...) and VALUE2 in (...) ... ")

 public static Specification<Table> filteredListOfValues(final FilterRequestsParametersBean filterRequestsParametersBean) { return new Specification<Table>() { public Predicate toPredicate(Root<Table> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { List<Predicate> listOfPredicates = createListOfPredicates(root, filterRequestsParametersBean); return criteriaBuilder.and(listOfPredicates.toArray(new Predicate[listOfPredicates.size()])); } }; } 

After that, I find the All () elements with the TableRepository and pageable parameters.

 this.getTableRepository().findAll(where(filteredListOfValues(filterRequestsParametersBean)), pageable); 

Type of table:

 id, value, etc.. 1, 1, ... 2, 1, ... 3, 2, ... 4, 2, ... 5, 3, ... 6, 5, ... 

And the question. How to select all values ​​other than VALUE in TABLE using Pageable.


Of course, I can use HashMap, but I will not have the means to print from Spring Data JPA. I need sorting and swapping.

I can also use the specification for a single Value field, for example:

  public static Specification<Long> filteredListOfValues(final FilterRequestsParametersBean filterRequestsParametersBean) { return new Specification<Long>() { public Predicate toPredicate(Root<Long> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { ....//somethind like that criteriaQuery.select(root.<Long> get("value")).distinct(true); .... List<Predicate> listOfPredicates = createListOfPredicates(root, filterRequestsParametersBean); return criteriaBuilder.and(listOfPredicates.toArray(new Predicate[listOfPredicates.size()])); } }; } 

but in this case, the findAll () method from the TableRepository repository can only accept the following parameters: JpaSpecificationExecutor<Table> , not JpaSpecificationExecutor<Long>

Thanks.

+4
source share

Source: https://habr.com/ru/post/1441209/


All Articles