- import java.util.Iterator;
- import java.util.List;
- import junit.framework.TestCase;
- import org.hibernate.Query;
- import org.hibernate.SQLQuery;
- import org.hibernate.Session;
- import cn.edu.ahau.mgc.hibernate.factory.HibernateSessionFactory;
- public class TestCount extends TestCase {
- public void test2() {
- Session session = null;
- try {
- session = HibernateSessionFactory.getSession();
- List<Object[]> results = session.createQuery("select c.id,c.name,count(s) from Student s join s.classes c group by c.id, c.name").list();
- for (Iterator<Object[]> iterator = results.iterator(); iterator.hasNext();) {
- Object[] result = iterator.next();
- Long countLong=(Long)result[2];//需转为long对象
- Integer count=Integer.parseInt(countLong.toString());
- System.out.println("----ID:" + result[0] + ";Name:" + result[1] + "Students Count:" + count);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- HibernateSessionFactory.closeSession();
- }
- }
- }
Hibernate中HQL的统计查询
最新推荐文章于 2024-05-06 18:26:10 发布