HQL 或 Java 持久性查询语言中的 IN 子句 [英] IN-clause in HQL or Java Persistence Query Language
本文介绍了HQL 或 Java 持久性查询语言中的 IN 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下参数化的 JPA 或 Hibernate 查询:
I have the following parametrised JPA, or Hibernate, query:
SELECT entity FROM Entity entity WHERE name IN (?)
我想将参数作为 ArrayList
I want to pass the parameter as an ArrayList<String>, is this possible? Hibernate current tells me, that
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
这可能吗?
ANSWER:作为参数的集合仅适用于诸如:name
"之类的命名参数,而不适用于诸如?
"之类的 JDBC 样式参数.
ANSWER: Collections as parameters only work with named parameters like ":name
", not with JDBC style parameters like "?
".
推荐答案
您使用的是 Hibernate 的 Query
对象,还是 JPA?对于 JPA,它应该可以正常工作:
Are you using Hibernate's Query
object, or JPA? For JPA, it should work fine:
String jpql = "from A where name in (:names)";
Query q = em.createQuery(jpql);
q.setParameter("names", l);
对于 Hibernate,您需要使用 setParameterList:
For Hibernate's, you'll need to use the setParameterList:
String hql = "from A where name in (:names)";
Query q = s.createQuery(hql);
q.setParameterList("names", l);
这篇关于HQL 或 Java 持久性查询语言中的 IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文