我在Spring Boot项目中使用JPA规范。所有标准都运行良好。但是问题出在选择/多选。
代码-
@AllArgsConstructor
public class TodoSpecifications implements Specification<Todo> {
@Override
public Predicate toPredicate(
Root<todoEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList = createPredicateList(root, criteriaBuilder);
List<Selection<? extends Object>> selectionList = new ArrayList<Selection<? extends Object>>();
Selection<? extends Object> selection = root.get("id");
selectionList.add(selection);
Selection<? extends Object> selection2 = root.get("name");
selectionList.add(selection2);
Selection<? extends Object> selection3 = root.get("type");
selectionList.add(selection3);
return query.multiselect(selectionList).where(criteriaBuilder.and(predicateList.toArray(new Predicate[] {}))).getRestriction();
}
}
使用此规范,我尝试了 列出toDoEntity = todoRepository.findAll(todoSpecification); 这将返回完整数据。但是我只希望选择的列。
答案 0 :(得分:0)
您是否有采用3个参数的构造函数?
为了能够使用多选,您必须像这样在TodoEntity
中创建一个构造函数
public TodoEntity (Long id, String name, String type) {
this.id = id;
this.name = name;
this.type = type;
}