HQL或Java持久性查询语言中的IN子句(IN

编程入门 行业动态 更新时间:2024-10-26 04:26:30
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.String

Is 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);

更多推荐

本文发布于:2023-07-24 01:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   持久性   语言   HQL   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!