HQL或Java持久性查询语言中的IN子句(IN-clause in HQL or Java Persistence Query Language)
我有以下参数的JPA或Hibernate,查询:
SELECT entity FROM Entity entity WHERE name IN (?)我想将参数作为ArrayList <String>传递,这是可能的吗? Hibernate当前告诉我,那个
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String这是可能的吗
解答 :作为参数的集合只能使用诸如“ :name ”之类的命名参数,而不是像“ ? ”这样的JDBC样式参数。
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.StringIs this possible at all?
ANSWER: Collections as parameters only work with named parameters like ":name", not with JDBC style parameters like "?".
最满意答案
你使用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);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);更多推荐
发布评论