在@Query中将@Param作为List跳过如果在Spring Data JPA中为null:antlr.NoViableAltException:意外的AST节点

时间:2018-01-26 09:35:14

标签: java hibernate jpa spring-boot spring-data-jpa

UPD

    @Query(value = "select distinct d.documentId " +
        "from TrainingDocument d " +
        "where (:docTypes is null or d.documentTypeName in :docTypes)")
List<String> findDocByDocTypes(
        @Param("docTypes") List<String> docTypes);

我有一个类似上面的查询,我在其中检查List<String> docTypes是否为null。使用docTypes == null或列表中只有一个元素可以使用。只要我有多个元素,我就会得到:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector} 
[select distinct d.documentId from com.example.model.TrainingDocument d 
where (:docTypes_0_, :docTypes_1_ is null or d.documentTypeName in
(:docTypes_0_, :docTypes_1_))]; nested exception is
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector} 
[select distinct d.documentId from com.example.model.TrainingDocument d
where (:docTypes_0_, :docTypes_1_ is null or d.documentTypeName in
(:docTypes_0_, :docTypes_1_))]

我已经找到了这个解决方案How to skip @Param in @Query if is null or empty in Spring Data JPA 它描述了我的情况,但对我不起作用。 我正在使用spring-boot 1.5.9.RELEASE

1 个答案:

答案 0 :(得分:0)

如果向量为空,则可以产生null;否则,可以产生第一个值

where (coalesce(:docTypes, null) is null or d.documentTypeName in :docTypes)
相关问题