Hibernate中Query.uniqueResult();

来源:互联网 发布:java基础笔记 编辑:程序博客网 时间:2024/05/16 05:06
如果有多个值抛错
如果有值且只有一个,返回一个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();
            }*/
原创粉丝点击