【转】Hibernate获取记录总数

hql获取记录条数

String hql = "select count(ci.channelid) as count from ChannelInfo ci";
       query = arg0.createQuery(hql);
       count = ((Number) query.iterate().next()) .intValue();
 
 

Criteria获取记录条数

totalPage = ((Number) crit.setProjection(
         Projections.rowCount()).uniqueResult()) .intValue();
 

sql获取记录条数

String sql = "select count(ca.CATALOGLID) as count from    
         PLATFORM_CATALOG_INFO ca, PLATFORM_CHANNEL_INFO ch where ch.CHANNELID= '"
         + channelid
         + "' and ca.CHANNELID = ch.CHANNELID  order by ca.CATALOGLID";
       return session.createSQLQuery(sql).addScalar("count",
         Hibernate.INTEGER).uniqueResult();
 

自己的例子:

//获取记录总数
	public static void getTotal()
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.getTransaction();
		tx.begin();
		
		Query query = session.createQuery(" select count(cat.id) from Category cat ");
		System.out.println(" Category 共有记录数 total = " 
                                    + ((Number)(query.iterate().next())).intValue());
	}
 

 

相关推荐