-
Hibernate查询有关 in 的查询问题3
public List<Document> getDocumentsByIds(final String ids){ List<Document> documents=getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException,SQLException { StringBuffer sql=new StringBuffer("from Document doc where doc.id in(?)"); Query query=session.createQuery(sql.toString()); query.setString(0,ids); return query.list(); } }); if(documents!=null&&documents.size()>0){ return documents; } return null; }
如上的代码,为什么我传递参数 "3,4,5",却只能查询出第一个3的对象,后面两个相应的对象为什么查询不出来,数据库都有记录存在的!
求解释
问题补充:visoin 写道List<String> ids= new ArrayList<String>(); ids.add()........
什么意思啊?2011年11月11日 12:47
3个答案 按时间排序 按投票排序
-
采纳的答案
你用的是HQL语句,不是SQL。
使用HQL的in语句时,传出的是集合或者数组。因此你需要使用以下方式:
List<Integer> ids = new ArrayList<Integer>();
ids.add(3);
ids.add(4);
ids.add(5);
Query query=session.createQuery(from document where id in (:ids));
query.setParameterList("ids", ids);
query.list();
如果你不想改动你的方法名,并且使用你的上述方法,那么你需要将
session.createQuery改成session.createSQLQuery。此时不能使用query.setString方法。你只能使用字符串拼接了。2011年11月14日 11:11
-
你的id是什么类型的,是number类型还是varchar类型的,如果是varchar类型的应该传的参数为"'3','4','5'"。
2011年11月14日 16:01
相关推荐
Hibernate Search in Action aims not only at providing practical knowledge of Hibernate Search but also uncovering some of the background behind Hibernate Search’s design. We will start by describing ...
hibernate Search in action
Hibernate In Action 3 第三部分 中文 入门教程
hibernate_in_action 源码
Hibernate In Action 电子书 pdf
hibernate in action 是绝对的中文版,完整版!!!
Hibernate In Action中文版电子书
Hibernate Search in action (pdf && code)
Hibernate In Action(Java.Persistence.with.Hibernate)的源码,从该书的出版社网站上下载下来的.
Emmanuel Bernard joined the Hibernate team in 2003 and is now the lead developer of Hibernate Annotations and Hibernate EntityManager, two key projects on top of the Hibernate core implementing the ...
hibernate in action 第二版中文版 j2ee课程教学参考书籍
hibernate in action 中文版书籍
本资源为Hibernate in action中文完整版,共635页,正文内容608页。分为三个分卷,全部下载可正常解压缩。 本书为Hibernate之父亲自执笔,无可替代的Hibernate和ORM技术圣经,职业Java程序员必备秘籍。
As a major revision of the popular Hibernate in Action, it builds on the same single example application to introduce and explain the latest Hibernate 3.2 in detail. In addition, the new and ...
hibernate in actionhibernate in actionhibernate in action
Action Hibernate In 中文 入门教程
Hibernate in action Spring in Action Struts In Action 全是英文版
方便网友们下载,特此做了个汇总,文件包括(共4个): 中文版《Hibernate实战》 - 电子版(部分) 中文版《Hibernate实战(第2版)》-扫描版 英文版《Java Persistence With...英文版《Hibernate in action》- 电子版