日期:2014-05-17  浏览次数:20724 次

大大们,求个hibernate DetachedCriteria分组查询
目标sql是这样的:
select  toolName ,count(*) as c from Km_Toolclick_Cnt where 1=1 group by toolName;


现在换成这样的写法:

DetachedCriteria dc = DetachedCriteria.forClass(KmToolclickCnt.class);
if(!ValidateUtils.isEmpty(toolbarclick.getToolName())){
dc.add(Restrictions.ilike("toolName", toolbarclick.getToolName()));
}
if(!ValidateUtils.isEmpty(beginDate)){
dc.add(Restrictions.gt("goTime", beginDate));
}
if(!ValidateUtils.isEmpty(endDate)){
dc.add(Restrictions.lt("goTime", endDate));
}

//dc.setProjection(Property.forName("toolName").group());
dc.setProjection(Projections.alias(Projections.groupProperty("toolName"),"toolName"));
//dc.setProjection(Projections.alias(Projections.rowCount(),"count"));
hibernate sql

------解决方案--------------------
不知其他人是怎么样,反正我是没看明白你想干什么的?
------解决方案--------------------
dc.addOrder(Order.desc("toolName"));
------解决方案--------------------
这样试下呢
DetachedCriteria dc = DetachedCriteria.forClass(KmToolclickCnt.class);
dc.setProjection(Property.forName("toolName").group());

据说貌似是个bug
------解决方案--------------------
DetachedCriteria dc = DetachedCriteria.forClass(KmToolclickCnt.class);
        if(!ValidateUtils.isEmpty(toolbarclick.getToolName())){
            dc.setProjection(Projections.rowCount())   //1
                 .add( Projections.groupProperty("toolName")).list();   //2
        }


这样你试试,1是统计count(*)   2是根据"toolName"分组