2

我需要使用 JPA Criteria API 编写以下内容:

p.id IS NULL  AND   nv.organizationalstat IN ('EMPLOYEE', 'FELLOW')`

我的代码是:

 List<String> corePredicateInList = Arrays.asList("EMPLOYEE", "FELLOW");
 In<String> corePredicateIn = cb.in(nvRoot.get("organizationalstat"));
 corePredicateInList.forEach(p -> corePredicateIn.value(p)); // Setting opts into IN
 Predicate p1CorePredicate = cb.and( 
                                cb.isNull(plans.get("id")), 
                                cb.in(nvRoot.get("organizationalstat"), corePredicateIn)
                            );

运行时错误是

antlr.NoViableAltException: unexpected token: in
...
where ( ( ( ( ( ( generatedAlias0.id is null ) 
  and ( generatedAlias1.organizationalstat in (:param0, :param1) in () ) ) 
  or ( generatedAlias0.id is not null ) )

看起来某处有双输入。代码中没有语法错误。

我不能cb.in(List<String>)直接做,这是错误的语法。我必须in.value()按照此线程中的说明进行操作。

4

2 回答 2

2

使用CriteriaBuilder.In

Predicate p1CorePredicate = cb.and(cb.isNull(plans.get("id")),corePredicateIn);

或使用Expression.In

Predicate p1CorePredicate = cb.and(cb.isNull(plans.get("id")), 
                         nvRoot.get("organizationalstat").in(corePredicateInList));
于 2020-06-26T18:18:57.360 回答
1

对我有用的解决方案是root.in(List<String>)(不是cb.in),如下所示:

 List<String> corePredicateInList = Arrays.asList("EMPLOYEE", "FELLOW");
 Predicate p1CorePredicate = cb.and( 
                                cb.isNull(plans.get("id")), 
                                nvRoot.get("organizationalstat").in(corePredicateInList)
                            );
于 2020-06-26T18:25:55.120 回答