Java JPA EntityManager createQuery() 与 IN 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3113611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA EntityManager createQuery() with IN not working
提问by gmoore
This is failing:
这是失败的:
List<String> names = new ArrayList<String>();
names.add("sold");
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN (?)");
query.setParameter(1, names);
List<PropField> fields = query.getResultList();
And so is this:
这是这样的:
List<String> names = new ArrayList<String>();
names.add("sold");
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN (?)");
query.setParameter(1, names.toArray());
List<PropField> fields = query.getResultList();
This, too:
这个也是:
List<String> names = new ArrayList<String>();
names.add("sold");
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN ?");
query.setParameter(1, names.toArray());
List<PropField> fields = query.getResultList();
And every other permutation of the above. Checked the docs and it says the first option should work. Here's the exception for the top one.
以及上述所有其他排列。检查了文档,它说第一个选项应该有效。这是顶级的例外。
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
at org.hibernate.type.StringType.toString(StringType.java:67)
Hibernate's HQL uses setParameterList
, but trying to stick with straight JPA here.
Hibernate 的 HQL 使用setParameterList
,但试图在这里坚持使用直接的 JPA。
采纳答案by Mike Hopper
I ran into a similar problem with Hibernate using a JPA named query with an IN clause. I got it working with the syntax: "propField.name IN (?1)"
我使用带有 IN 子句的 JPA 命名查询遇到了类似的 Hibernate 问题。我得到了它的语法:“propField.name IN (?1)”
Also see: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4922
另见:http: //opensource.atlassian.com/projects/hibernate/browse/HHH-4922
and http://opensource.atlassian.com/projects/hibernate/browse/HHH-5126
和http://opensource.atlassian.com/projects/hibernate/browse/HHH-5126
回答by Andy
I don't have access to a Hibernate environment to try this, but have you tried
我无法访问 Hibernate 环境来尝试此操作,但是您是否尝试过
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN elements(?)");
query.setParameter(1, names);
回答by Pascal Thivent
From the JPA 1.0 specification:
从 JPA 1.0 规范:
4.6.4.1 Positional Parameters
The following rules apply to positional parameters.
- Input parameters are designated by the question mark (?) prefix followed by an integer. For example:
?1
.- Input parameters are numbered starting from 1.
Note that the same parameter can be used more than once in the query string and that the ordering of the use of parameters within the query string need not conform to the order of the positional parameters.4.6.4.2 Named Parameters
A named parameter is an identifier that is prefixed by the ":" symbol. It follows the rules for identifiers defined in Section 4.4.1. Named parameters are case sensitive.
Example:
SELECT c FROM Customer c WHERE c.status = :stat
Section 3.6.1 describes the API for the binding of named query parameters
4.6.4.1 位置参数
以下规则适用于位置参数。
- 输入参数由问号 (?) 前缀后跟一个整数指定。例如:
?1
。- 输入参数从 1 开始编号。
请注意,同一参数可以在查询字符串中多次使用,并且查询字符串中参数的使用顺序不需要与位置参数的顺序一致。4.6.4.2 命名参数
命名参数是以“:”符号为前缀的标识符。它遵循第 4.4.1 节中定义的标识符规则。命名参数区分大小写。
例子:
SELECT c FROM Customer c WHERE c.status = :stat
第 3.6.1 节描述了用于绑定命名查询参数的 API
So either use (with a named parameter):
所以要么使用(带有命名参数):
List<String> names = Arrays.asList("sold");
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN (:names)");
query.setParameter("names", names)
List<PropField> fields = query.getResultList();
Or (with a positional parameter):
或(带有位置参数):
List<String> names = Arrays.asList("sold");
Query query = em.createQuery("FROM PropField propField WHERE propField.name IN (?1)");
query.setParameter(1, names)
List<PropField> fields = query.getResultList();
Both tested with Hibernate EM 3.4.0.GA.
两者都使用 Hibernate EM 3.4.0.GA 进行了测试。
Note that being forced to use parenthesis to surround the parameter of the IN clause is a bug (at least in JPA 2.0) as outlined in this previous answer.
请注意,强制使用括号来包围 IN 子句的参数是一个错误(至少在 JPA 2.0 中),如上一个答案所述。