首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPA QuerySyntaxException:意外的AST节点:{vector}用于条件

JPA QuerySyntaxException:意外的AST节点:{vector}用于条件
EN

Stack Overflow用户
提问于 2016-08-28 19:52:51
回答 4查看 17.9K关注 0票数 11

我正在尝试将IN操作与@Query注释与JPA结合使用。我正在犯以下错误:-

代码语言:javascript
复制
    antlr.NoViableAltException: unexpected AST node: {vector}
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2112)

    org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: 
    unexpected AST node: {vector}
 [ [select o FROM Stock o where (:productlist_0_, :productlist_1_, :productlist_2_, :productlist_3_ is null or o.productsid IN (:productlist_0_, :productlist_1_, :productlist_2_, :productlist_3_) )

我的Java代码如下:

代码语言:javascript
复制
@Query("SELECT o FROM Stock o  where (:productlist is null or o.productsid IN (:productlist) ) ")
List<Stockdiary> getAllStock(Pageable pageable, @Param("productlist") List<Products> productlist)

--当我在productlist中只有一个条目时,它可以很好地工作。但是,当我在productlist中有多个条目时,查询如下,并出现错误:-

代码语言:javascript
复制
select o FROM Stock o where (:productlist_0_, :productlist_1_, :productlist_2_, :productlist_3_ is null or o.productsid IN (:productlist_0_, :productlist_1_, :productlist_2_, :productlist_3_

我已经看过这个link了,但是这个解决方法并不适合我。我试着传递带括号和不带括号的:productlist

EN

回答 4

Stack Overflow用户

发布于 2017-05-10 11:33:30

我和你一样有同样的问题,解决办法很容易。工作之后,我发现下一个变体很好:

代码语言:javascript
复制
@Query("select c from Cruise c where" +
        " (:categoryId is null or c.category.id = :categoryId)" +
        " and ((:portsIds) is null or c.port.id in (:portsIds))")
List<Cruise> findByRequestQuery(@Param("portsIds") List<Long> portsIds,
                                @Param("categoryId") Long categoryId);

如前所述:https://stackoverflow.com/a/24551530/6629515应该将括号添加到集合参数中,对于我来说,如果将括号添加到每个集合参数提到:

代码语言:javascript
复制
(:portsIds)

以及结果SQL查询,其中部件看起来如下:

代码语言:javascript
复制
where (? is null or cruise0_.category_id=?) and ((? , ? , ?) is null or cruise0_.port_id in (? , ? , ?))

对于操作数应该包含1列问题

请阅读此处:https://stackoverflow.com/a/51958547/1522490

简而言之:使用coalesce(:portsIds, null) is null

票数 19
EN

Stack Overflow用户

发布于 2019-07-27 07:13:20

因为我不能评论亚历克斯?埃夫莫夫的回答,所以我就把答案写在阿历克斯?埃夫莫夫的基础上。

关于@Cat H的问题( SQLException:操作数应该包含1列(S))

您可以使用这种方法(在空集合时添加一个布尔变量来忽略查询):

基于@Alex Efimov的代码:

代码语言:javascript
复制
boolean ignoreQueryPortsIds = CollectionUtils.isEmpty(portsIds);
@Query("select c from Cruise c where" +
        " (:categoryId is null or c.category.id = :categoryId)" +
        " and (:ignoreQueryPortsIds is true or c.port.id in :portsIds)")
List<Cruise> findByRequestQuery(@Param("portsIds") List<Long> portsIds,
                                @Param("categoryId") Long categoryId,
                        @Param("ignoreQueryPortsIds ") boolean ignoreQueryPortsIds );

注意:您必须在is true之后添加:ignoreQueryPortsIds,否则jpa无法解析您的HQL

票数 0
EN

Stack Overflow用户

发布于 2020-03-18 11:54:17

我面临同样的问题,我修复了它,以扭转o.productsid IN (:productlist) OR :productlist is null条件

代码语言:javascript
复制
@Query("SELECT o FROM Stock o  where (o.productsid IN (:productlist) OR :productlist is null ) ")

List<Stockdiary> getAllStock(Pageable pageable, @Param("productlist") List<Products> productlist)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39195200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档