首页 > 代码库 > hibernate查询之聚合函数

hibernate查询之聚合函数

技术分享

1. 获取总的记录数
    Session session = HibernateUtils.getCurrentSession();
    Transaction tr = session.beginTransaction();
    List<Number> list = session.createQuery("select count(c) from Customer c").list();
    Long count = list.get(0).longValue();
    System.out.println(count);
    tr.commit();

2. 获取某一列数据的和
    Session session = HibernateUtils.getCurrentSession();
    Transaction tr = session.beginTransaction();
    List<Number> list = session.createQuery("select sum(c.cust_id) from Customer c").list();
    Long count = list.get(0).longValue();
    System.out.println(count);
    tr.commit();

hibernate查询之聚合函数