gpt4 book ai didi

java - JPA 查询语法异常 : unexpected AST node: {vector} For In condition

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:24:49 25 4
gpt4 key购买 nike

我正在尝试将 IN 操作与 @Query 注释和 JPA 一起使用。我遇到以下错误:-

    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 代码如下:-

@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 中有多个项目时,查询如下所示并且错误:-

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

最佳答案

我遇到了和你一样的问题,解决方法很简单。变通后我发现下一个变体工作正常:

@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你应该在集合参数中添加括号,对我来说,如果在每个集合参数中添加括号,它就有效:

(:portsIds)

结果 SQL 查询部分如下所示:

where (? is null or cruise0_.category_id=?) and ((? , ? , ?) is null or cruise0_.port_id in (? , ? , ?))

针对Operand should contains 1 column问题

请阅读这里:https://stackoverflow.com/a/51958547/1522490

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

关于java - JPA 查询语法异常 : unexpected AST node: {vector} For In condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195200/

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