1

I am using the specification and findAll method to retrieve the paginated results that matches to given criteria.

'findAll(spec, pageRequest)' method of 'org.springframework.data.jpa.repository.JpaSpecificationExecutor' interface.

I defined Specification as below.

public static <T> Specification<T> bySearchFilter(final Class<T> clazz) {
    return (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
        query.multiselect(root.get("id")).distinct(true);

        List<Predicate> predicates = Lists.newArrayList();

        predicates.add(builder.equal(root.get("active"), "Y"));

        return builder.and(predicates.toArray(new Predicate[0]));

    };
}

query.multiselect(root.get("id"), root.get("name")).distinct(true);

Above snippet giving distinct results but selecting all the columns of the table. But I want only id and name columns.

Am I doing any mistake here?

Hari Krishna
  • 3,658
  • 1
  • 36
  • 57

1 Answers1

0

You can try the below code.

List<Selection<?>> selectionList = root.getCompoundSelectionItems();
selectionList.add(root.get("id"));
selectionList.add(root.get("name"));

query.multiselect(selectionList);
SSK
  • 3,444
  • 6
  • 32
  • 59
  • Getting error "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Not a compound selection; nested exception is java.lang.IllegalStateException: Not a compound selection", – Hari Krishna Aug 12 '20 at 13:26