一、hibernate中的分组查询
package edu.aeon.hibernate.test; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import edu.aeon.aeonutils.hibernate.getsessionutil.GetSessionUtil; import edu.aeon.beans.Student; /** * [说明]:分组条件查询:查询学生表中年龄段大于1的年龄集合 * @author aeon * */ public class TestQuery { @Test public void testQueryRecordCount() { Session session=null; Transaction transaction=null; try { session = GetSessionUtil.getSession(); transaction=session.getTransaction(); transaction.begin(); //这里的count(*)相当于count(id) String sql = "select stuAge from Student group by stuAge having count(stuAge)>1 "; List userList = session.createQuery(sql).list(); System.out.println(userList); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); //事物的回滚操作 transaction.rollback(); } } }
数据库数据信息截图:
运行结果截图:
可见 22和24这两个年龄段的人数各大于1