[url]http://blog.youkuaiyun.com/tanggoodboy/article/details/4161679[/url]
在hibernate 查询in 查询中不能使用testQuery1方法查询
应该使用如下方法:
[color=red]Integer id[] [/color]= new Integer[]{1,2,3,4};
String hql = "from Group as p where p.id in (:test)";
Query query = session.createQuery(hql);
query.[color=red]setParameterList[/color]("test", id);
两者的区别之处在蓝色字体部分。
在hibernate 查询in 查询中不能使用testQuery1方法查询
public void testQuery1(){
SessionFactory factory = null;
Session session = null;
factory = (SessionFactory) HibernateSessionFactory.getSessionFactory();
session = factory.openSession();
Integer id[] = new Integer[]{1,2,3,4};
String hql = "from Group as p where p.id in (?)";
Query query = session.createQuery(hql);
query.setParameter(1, id);
List list = query.list();
System.out.println(list.size());
session.close();
}应该使用如下方法:
public void testQuery2(){
SessionFactory factory = null;
Session session = null;
factory = (SessionFactory) HibernateSessionFactory.getSessionFactory();
session = factory.openSession();
Integer id[] = new Integer[]{1,2,3,4};
String hql = "from Group as p where p.id in (:test)";
Query query = session.createQuery(hql);
query.setParameterList("test", id);
List list = query.list();
System.out.println(list.size());
session.close();
}[color=red]Integer id[] [/color]= new Integer[]{1,2,3,4};
String hql = "from Group as p where p.id in (:test)";
Query query = session.createQuery(hql);
query.[color=red]setParameterList[/color]("test", id);
两者的区别之处在蓝色字体部分。
本文介绍了在Hibernate中使用IN查询的正确方式。通过对比两种不同的实现方法,指出了一种常见的错误做法,并给出了正确的实现方案。
816

被折叠的 条评论
为什么被折叠?



