Session session= HibernateSessionFactory.getSessionFactory().getCurrentSession(); try { session.beginTransaction(); Iterator it=session.createQuery("select sum(age),gx from Userinfo group by gx having count(*)>2").iterate(); while(it.hasNext()){ //TStudents t=(TStudents)it.next(); //System.out.println(t.getGx()+" "+t.getId().getName()); Object[] os=(Object[])it.next(); //String st=(String)os[0]; System.out.println(os[0]+" "+(String)os[1]); } session.getTransaction().commit(); } catch (HibernateException e) { session.getTransaction().rollback(); e.printStackTrace(); } }
Iterator 应该不能直接接Query,所以应先用list接过来,再迭代!如下: 但要注意sql语句,及组函数的用法 Iterator it=session.createQuery("select cid,sum(cid) from Customer group by cid having count(*)>2").list().iterator(); while(it.hasNext()){ Customer c = (Customer)it.next(); System.out.println("-------------"+c.getCname()); }
try {
session.beginTransaction();
Iterator it=session.createQuery("select sum(age),gx from Userinfo group by gx having count(*)>2").iterate();
while(it.hasNext()){
//TStudents t=(TStudents)it.next();
//System.out.println(t.getGx()+" "+t.getId().getName());
Object[] os=(Object[])it.next();
//String st=(String)os[0];
System.out.println(os[0]+" "+(String)os[1]);
}
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
}
}
Iterator it=session.createQuery("select cid,sum(cid) from Customer group by cid having count(*)>2").list().iterator();
while(it.hasNext()){
Customer c = (Customer)it.next();
System.out.println("-------------"+c.getCname());
}