『hibernate学习』hibernate最原始的方法取得count(*)

    
Hibernate 如何使用count(*)
public int getCount(String emailGroupId, String emailBatchId)
            throws HibernateException {

        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();

        String hql = "select count(*) from EmailSendInfo where email_group_id = :emailGroupId and batch_id = :batchId";
        Query query = session.createQuery(hql);

        query.setString("emailGroupId", emailGroupId);
        query.setString("batchId", emailBatchId);

        for (Iterator it = query.iterate(); it.hasNext();) {
            return ((Integer) it.next()).intValue();
        }
        return 0;
    }


Spring+Hibernate 如何使用count(*)
 
第一种方法:
String hql = "select count(*) from User as user";
Integer count = (Integer)getHibernateTemplate().find(hql).listIterator().next();
return count.intValue();
第二种方法:
String hql = "select count(*) from User as user";
return ((Integer)getHibernateTemplate().iterate(hql).next()).intValue();
第三种方法:
String hql = "select count(*) from User as user";
Query query = getHibernateTemplate().createQuery( getSession(),hql);
return ((Integer)query.uniqueResult()).intValue();  

你可能感兴趣的:(java,spring,Hibernate)