处理“NOT IN (:param)”时的 JPA setParameter
我正在尝试在查询中设置一个参数,例如:
select * from Cars where Cars.color NOT IN (:color_params)
当我在 JavaClass 中添加参数时,就像:
...
query.setParameter("color_params", "RED,BLUE");
...
这不起作用,仅适用于仅一个参数。
我尝试过 "'RED','BLUE'"
并且不工作。
如果我将参数放入查询中,例如:
select * from Cars where Cars.color NOT IN ('RED','BLUE')
我做错了什么?
I'm trying to set a parameter in my query, for example:
select * from Cars where Cars.color NOT IN (:color_params)
And when I'm adding the parameter in my JavaClass is like:
...
query.setParameter("color_params", "RED,BLUE");
...
And this is not working, is only working with only one parameter.
I've tried with "'RED','BLUE'"
and is not working to.
If I put my parameters in the query is working for example:
select * from Cars where Cars.color NOT IN ('RED','BLUE')
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该传递一个列表。
您还可以这样做:
作为一般规则,通常更喜欢将参数传递给固定查询,而不是自定义字符串。优点可能是:
You are supposed to pass a List.
You could also do:
As a general rule, it is often prefered to pass parameters to a fixed query, instead of customizing the String. The advantages could be:
您必须传入字符串列表,而不是单个字符串。 JPA 不会解析您的值,您必须自己拆分它们。
You must pass in a list of strings, not a single string. JPA doesn't parse your values, you must split them yourself.