如何使用Hibernate从mysql获取单个记录的数量

时间:2017-01-06 09:58:11

标签: mysql hibernate

我正在尝试使用hibernate从mysql数据库中获取所有单个记录的计数,我必须将其存储在地图集合中。怎么做?

try {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();
    String hql = "select attendencestatus ,COUNT(attendencestatus) from studentattendance group by attendencestatus ";
    Query query = session.createQuery(hql);     
    tx.commit();

} catch (HibernateException he) {
    he.printStackTrace();
    tx.rollback();
}

1 个答案:

答案 0 :(得分:1)

--------------------------------------
|   ID    |CountryName|  CurrencyName|
|_________|___________|______________|
|   1     | India     |   Rupees     |
|   2     | India     |   Rupees     |
|   3     | India     |   Rupees     |
|   4     | Usa       |   dollar     |
|   5     | dubai     |   AED        |
|   6     | Germany   |   Euro       |
|--------------------------------------

String hql="select anyColmnName from entity;
 Query query = session.createQuery(hql);
 List<string> list=query.list();
int countOfRecords=Collections.frequency(list, value)

示例

  String hql="select  countryName from Revenue;
     Query query = session.createQuery(hql);
     List<string> list=query.list();
    int countOfRecords=Collections.frequency(list, "India")