• 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();   
  • 相关阅读:
    python-函数作用域
    python-yield
    python-内置函数
    python-迭代和递归
    Command /usr/bin/codesign failed with exit code 1
    vue-infinite-scroll 自动加载
    git登陆迁移 SourceTree 不能自动识别
    Xcode不自动提示代码
    NSTimer的循环引用
    iOS autolayout 代码,自定义间距
  • 原文地址:https://www.cnblogs.com/bjanzhuo/p/3575970.html
Copyright © 2020-2023  润新知