我只需要查询条目,而不需要里面的内容如果通过
createCriteria(xxx.class)
再返回list.size(),数据量大对数据库有压力请问有没有类似的select count(*) from xxx后面加条件的实现?能否将这段代码优化下??public Integer findCount(final Integer moduleId,final String rightName) throws DaoException {
return (Integer) hibernateTemplate.execute(new HibernateCallback(){ public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria=session.createCriteria(Right.class);
if(moduleId!=null){
criteria.add(Restrictions.eq("moduleId", moduleId));
}
if(rightName!=null && !rightName.equals("")){
criteria.add(Restrictions.eq("name",rightName));
}
return criteria.list().size();
}

});
}

解决方案 »

  1.   

    直接用HQL语句好了啊   select count(e) from Right e where e. moduleId = : moduleId
      再把值给它  
      

  2.   

    select count(*) from 对象名 别名 where 别名.XX = ?
      

  3.   

    select cl.name,
    count(*) 
    FROM NetworkCabinets nc,
    ComputerLocation cl 
    WHERE nc.compLocaId=cl.id
    and nc.compLocaId=:compLocaId
    group by cl.name 
      

  4.   

    用hqlselect count(*) from JAVABEAN名用Query查询出来然后调用list();方法 get(0)就是了 
      

  5.   

    DetachedCriteria dc = DetachedCriteria.forClass(Right.class);
    dc.setProjection(Projections.projectionList().add(
    Projections.count("id")));