0

我正在使用SpringBoot 2.2.6withJPA并且我需要使用IN标题中提到的子句进行查询。我尝试过:

@Override
public Predicate toPredicate(Root<Distinta> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    
    List<Predicate> predicates = new ArrayList<>();
    .....
    .....
    for (DistintaCriteria criteria : list) {
        switch(criteria.getOperation()) {
        case TEST:
            Join<Entity, JoinEntity> join = root.join("joinEntity");
            predicates.add(join.<Integer>get("id").in(criteria.getValue()));
    }
}

wherecriteria.getValue()是一个Integer[]数组,但它不起作用。你能帮助我吗?

谢谢你们。

更新

如果我尝试同样Query的方法,List<String>它会起作用!使用整数我有这个错误:

Unaware how to convert value [[2, 3, 4, 5] : java.util.ArrayList] to requested type [java.lang.Integer]
4

2 回答 2

0

对于 in 子句,我们需要始终传递一个列表。

您需要将您的转换Integer arrayInteger list使用Java-8like

List<Integer> values = Arrays.asList(criteria.getValue())

@Override
public Predicate toPredicate(Root<Distinta> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    
    List<Predicate> predicates = new ArrayList<>();
    .....
    .....
    for (DistintaCriteria criteria : list) {
    
        List<Integer> values = Arrays.asList(criteria.getValue());
    
        switch(criteria.getOperation()) {
        case TEST:
            Join<Entity, JoinEntity> join = root.join("joinEntity");
            predicates.add(join.<Integer>get("id").in(values));
    }
}

在 Eclipse 中,如果我们传递一个数组,我们会收到警告

Type Integer[] of the last argument to a method in(Object...) doesn't exactly match the vararg parameter type. Cast to Object[] to confirm the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation.

于 2020-08-13T11:22:40.120 回答
0

我已解决如下:

Join<Entity, JoinEntity> join = root.join("joinEntity");
Predicate in = join.get("id").in((List<Integer>)criteria.getValue());
predicates.add(in);

我不知道为什么List<String>我不需要投射。希望有所帮助。

于 2020-08-07T07:27:29.157 回答