JPA 条件 API "IN" 谓词无效

JPA Criteria API "IN" Predicate not working

我需要用 JPA 条件编写以下内容 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 ) )

好像某处有双 IN。代码中没有语法错误。

我不能直接执行 cb.in(List<String>),这是错误的语法。我必须在这个线程中通过 in.value() 作为 indicated

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

一个对我有用的解决方案是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)
                            );