Hibernate中Query.uniqueResult();

如果有多个值抛错
如果有值且只有一个,返回一个object
如果没值,返回null
得到记录数Projections
ProjectionList projList = Projections.projectionList();
            projList.add(Projections.rowCount());
            if (groupProperty != null) {
                for (String pro : groupProperty) {
                    projList.add(Projections.groupProperty(pro));
                }
            }
            Integer count = (Integer) criteria
                    .setProjection(projList).uniqueResult();
注:当groupProperty的值不为0时则生成以下代码

select count(*) as y0_, this_.F_USER_IP as y1_   from
        T_ONLINE_SESSION this_  where ( this_.F_CORP_CODE=?  and this_.F_TYPE=? )
    group by this_.F_USER_IP
之后报如下异常:
at com.dayee.wintalent.framework.dao.HbmPageEntityDaoImpl$SizeCallbackImpl.doInHibernate(HbmPageEntityDaoImpl.java:596)
 at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
 at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
 at com.dayee.wintalent.framework.dao.HbmPageEntityDaoImpl.sizeEntity(HbmPageEntityDaoImpl.java:156)

进行跟踪发现出错行为(Integer) criteria .setProjection(projList).uniqueResult();原来是因为groupProperty的值不为0时,select语句得出的为数据表的两列故出错,之后改为以下代码
改为:
 /* Integer count = null;
           
            if (groupProperty.length>0) {
                ProjectionList projList = Projections.projectionList();
                for (String pro : groupProperty) {
                    projList.add(Projections.groupProperty(pro));
                }
                count = criteria.setProjection(projList).list().size();
            }else{
            count = (Integer) criteria.setProjection(Projections.rowCount())
                    .uniqueResult();
            }*/

你可能感兴趣的:(Hibernate,session,String,user,null,Integer)