HQL或Java持久性查询语言中的IN子句 [英] IN-clause in HQL or Java Persistence Query Language
本文介绍了HQL或Java持久性查询语言中的IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT实体FROM实体实体WHERE name IN(?)
我想将参数作为ArrayList< String>传递,这可能吗? Hibernate current告诉我,那
java.lang.ClassCastException:java.util.ArrayList不能转换为java.lang.String
这可能吗?
ANSWER :作为参数的集合只能用于名称参数,如:name
,而不是JDBC样式参数,如? 。
解决方案 你使用Hibernate的
Query
对象,还是JPA?对于JPA,它应该可以正常工作: String jpql =from A where name in(:names);
Query q = em.createQuery(jpql);
q.setParameter(names,l);
对于Hibernate,您需要使用setParameterList:
String hql =from A where name in(:names);
Query q = s.createQuery(hql);
q.setParameterList(names,l);
I have the following parametrised JPA, or Hibernate, query:
SELECT entity FROM Entity entity WHERE name IN (?)
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
Is this possible at all?
ANSWER: Collections as parameters only work with named parameters like ":name
", not with JDBC style parameters like "?
".
解决方案
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);
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屋!
查看全文