我的项目是求某一列的和,这列是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;
}
}