Hibernate查询总的记录数

1. 原生sql

String hql="select count(*) from product" ;//此处的product是数据库中的表名 
Query query=session.createSQLQuery(hql); 
List<BigInteger> list=query.list(); //此处胡一定要注意是Bignter类型的,傻了吧唧的试了好多(Long,Integer)都报错 
count = list.get(0).intValue();

 

2.hql语句

 String hql="select count(*) from Product";//此处的Product是对象
 Query query = session.createQuery(hql);
 count=((Long) query.setCacheable(true).uniqueResult()).intValue();//此处用Long类型进行转换

 

 

 

posted on 2014-12-19 10:11  到处都要注册  阅读(4030)  评论(0编辑  收藏  举报

导航