hibernate使用带有sum()的hql语句出错的问题

来源:互联网 发布:蓝海文交软件下载 编辑:程序博客网 时间:2024/06/06 00:19

我的项目是求某一列的和,这列是float类型 ,debug发现是返回值强转为float出错,于是我先转成String,再解析成float,问题解决。

正确代码如下:

public Float findAllHourByType(String studentId,String hourType) {        log.debug("getting Events All hourIn with property: " + studentId);        try {            String queryString = "select sum(evt_hour) from Events where stu_id = ? and evt_type= ?";            Query queryObject = getCurrentSession().createSQLQuery(queryString);            queryObject.setParameter(0, studentId);            queryObject.setParameter(1, hourType);            List ul = queryObject.list();            if (ul.get(0)!=null) {                String result = ul.get(0).toString();                 return Float.parseFloat(result);            } else {//如果结果为空返回结果为0                Float a=new Float(0);                return  a;            }        } catch (RuntimeException re) {            log.error("get failed", re);            throw re;        }    }
0 0
原创粉丝点击