提问人:askwer 提问时间:6/30/2023 最后编辑:Dmitriy Popovaskwer 更新时间:6/30/2023 访问量:87
存储库中的 JPA 查询命名参数问题
JPA Query named parameter issue in Repository
问:
我的存储库中有此方法:
@Query("""
SELECT items FROM ItemEntity items WHERE items.type = :type
AND (:idList IS NULL OR items.id IN :idList)
""")
List<Items> findItems(@Param("type") String type, @Param("idList") List<String> idList);
当是或只有 1 个项目时它有效,但它在包含超过 1 个项目时给了我一个:idList
null
IndexOutOfBoundsException
idList
java.lang.IndexOutOfBoundsException: Index: 0
at java.base/java.util.Collections$EmptyList.get(Collections.java:4586) ~[na:na]
我不知道是什么原因造成的。
答:
1赞
Mar-Z
6/30/2023
#1
添加括号,如下所示:
@Query("""
SELECT items FROM ItemEntity items WHERE items.type = :type
AND (:idList IS NULL OR items.id IN (:idList))
""")
评论
0赞
askwer
7/1/2023
试过这个,也是一样的 😩
0赞
Mar-Z
7/1/2023
请在类似情况下检查我的答案:stackoverflow.com/questions/76593358/...希望它有所帮助。
0赞
askwer
7/3/2023
嗨 @Mar-Z,coalesce(:idList) 起作用了。谢谢
0赞
Mar-Z
7/3/2023
凉。感谢您的反馈。我已经看到了coalesce(:idList)解决方案并尝试过,但它对我不起作用。至少不是 H2 数据库。
评论