gpt4 book ai didi

java - 如果在 Spring Data JPA 中为 null,则跳过 @Param 作为 @Query 中的列表 : antlr. NoViableAltException:意外的 AST 节点

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:46 25 4
gpt4 key购买 nike

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

最佳答案

如果你的 vector 为空,你可以产生 null,如果不是,则产生第一个值

where (coalesce(:docTypes, null) is null or d.documentTypeName in :docTypes)

关于java - 如果在 Spring Data JPA 中为 null,则跳过 @Param 作为 @Query 中的列表 : antlr. NoViableAltException:意外的 AST 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48458719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com