我正在尝试将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。
发布于 2017-05-10 11:33:30
我和你一样有同样的问题,解决办法很容易。工作之后,我发现下一个变体很好:
@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 (? , ? , ?))对于操作数应该包含1列问题
请阅读此处:https://stackoverflow.com/a/51958547/1522490
简而言之:使用coalesce(:portsIds, null) is null
发布于 2019-07-27 07:13:20
因为我不能评论亚历克斯?埃夫莫夫的回答,所以我就把答案写在阿历克斯?埃夫莫夫的基础上。
关于@Cat H的问题( SQLException:操作数应该包含1列(S))
您可以使用这种方法(在空集合时添加一个布尔变量来忽略查询):
基于@Alex Efimov的代码:
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
发布于 2020-03-18 11:54:17
我面临同样的问题,我修复了它,以扭转o.productsid IN (:productlist) OR :productlist is null条件
@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)https://stackoverflow.com/questions/39195200
复制相似问题