JPA规范:如何获取选定的列?

问题描述

我正在使用规范和findAll方法来检索与给定条件匹配的分页结果。

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

我定义了以下规范。

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);

以上片段给出了不同的结果,但选择了表的所有列。但是我只想要id和name列。

在这里犯错了吗?

解决方法

您可以尝试以下代码。

Type mapType = new TypeToken<LinkedMultiValueMap<Integer,CSVModelAttributes>>(){}.getType();
attrib = new Gson().fromJson(csvModel.get().getAttributes()),mapType );