zoukankan      html  css  js  c++  java
  • Hibernate 如何使用count(*)

    Java代码 复制代码 收藏代码
    1. public int getCount(String emailGroupId, String emailBatchId)
    2. throws HibernateException {
    3. Session session = HibernateUtil.currentSession();
    4. Transaction tx = session.beginTransaction();
    5. String hql = "select count(*) from EmailSendInfo where email_group_id = :emailGroupId and batch_id = :batchId";
    6. Query query = session.createQuery(hql);
    7. query.setString("emailGroupId", emailGroupId);
    8. query.setString("batchId", emailBatchId);
    9. /*
    10. * for (Iterator it = query.iterate(); it.hasNext();) { return
    11. * ((Integer) it.next()).intValue(); }
    12. */
    13. try {
    14. return ((Integer) query.iterate().next()).intValue();
    15. } catch (Exception e) {
    16. throw new HibernateException("");
    17. } finally {
    18. tx.commit();
    19. HibernateUtil.closeSession();
    20. }
    21. }



    Strings + Hibernate:
    Java代码 复制代码 收藏代码
    1. //第一种方法:
    2. String hql = "select count(*) from User as user";
    3. Integer count = (Integer)getHibernateTemplate().find(hql).listIterator().next();
    4. return count.intValue();
    5. //第二种方法:
    6. String hql = "select count(*) from User as user";
    7. return ((Integer)getHibernateTemplate().iterate(hql).next()).intValue();
    8. //第三种方法:
    9. String hql = "select count(*) from User as user";
    10. Query query = getHibernateTemplate().createQuery( getSession(),hql);
    11. return ((Integer)query.uniqueResult()).intValue();   
  • 相关阅读:
    LeetCode_Spiral Matrix II
    省选模拟10 题解
    省选模拟9 题解
    省选模拟8 题解
    省选模拟7 题解
    省选模拟6 题解
    省选模拟5 题解
    省选模拟4 题解
    数学专题测试3 题解
    数学专题测试2 题解
  • 原文地址:https://www.cnblogs.com/bjanzhuo/p/3575970.html
Copyright © 2011-2022 走看看