您的位置:首页 > 其它

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

2016-06-06 00:22 567 查看
我的项目是求某一列的和,这列是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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate hql