Hibernate 执行SQL语句的count函数返回BigDecimal类型
2012-03-23 14:21
573 查看
当前使用的Hibernate版本是3.2.6,数据库是Oracle11g,当使用 HibernateTemplate 的execute方法执行sql语句(不是 hql 语句)查询的时候,使用count聚合函数,返回结果是
BigDecimal 类型,不是Integer也不是Long,需强转成 BigDecimal 类型,然后调用其对象的 intValue 或 longValue 方法就可以了。
与上不同的是,当调用 HibernateTemplate 的 find 方法执行 hql 时,如果hql中有
count 函数,返回的结果是 Long 类型。
注:使用Criteria的count查询时返回的是Integer类型。
----------------------------------------------------------------------------------------------------------分割线-----------------------------------------------------------------------------------------------------------
将数据库改为MySQL5.1后,执行sql的count语句返回的是BigInteger类型,于是产生ClassCastException异常,为了不再产生类似问题,可将执行SQL语句返回的List<Object[ ]>中已知的数值类型强转成Number,然后调用Number的longValue(),inValue(),floatValue()方法得到想要的数值类型即可。
BigDecimal 类型,不是Integer也不是Long,需强转成 BigDecimal 类型,然后调用其对象的 intValue 或 longValue 方法就可以了。
与上不同的是,当调用 HibernateTemplate 的 find 方法执行 hql 时,如果hql中有
count 函数,返回的结果是 Long 类型。
注:使用Criteria的count查询时返回的是Integer类型。
public int getAlarmCountByFilter(final AlarmFilterBean filter,final int alarmCatelog) { return getHibernateTemplate().execute(new HibernateCallback<Integer>() { @Override public Integer doInHibernate(Session session) throws HibernateException, SQLException { Criteria c = session.createCriteria(ActiveAlarmBean.class); c.setProjection(Projections.rowCount()); addFilterRestrictions(c,filter,alarmCatelog); return (Integer)c.uniqueResult(); } }); }
----------------------------------------------------------------------------------------------------------分割线-----------------------------------------------------------------------------------------------------------
将数据库改为MySQL5.1后,执行sql的count语句返回的是BigInteger类型,于是产生ClassCastException异常,为了不再产生类似问题,可将执行SQL语句返回的List<Object[ ]>中已知的数值类型强转成Number,然后调用Number的longValue(),inValue(),floatValue()方法得到想要的数值类型即可。
final String sql = "select t.TEMPLATE_ID,count(d.TEMPLATE_ID) from RES_INPUT_TEMPLATE t left join "+tableName+" d" +" on t.TEMPLATE_ID = d.TEMPLATE_ID " +" where t.TEMPLATE_TYPE = "+templateType+" " +" group by t.TEMPLATE_ID"; Map<Long,Integer> idCountMap = new HashMap<Long, Integer>(); List<Object[]> list = inputTemplateDao.getHibernateTemplate().execute(new HibernateCallback<List<Object[]>>() { @SuppressWarnings("unchecked") public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException { return session.createSQLQuery(sql).list(); } }); for(Object[] result : list){ idCountMap.put(((Number)result[0]).longValue(), ((Number)result[1]).intValue()); }
相关文章推荐
- hibernate执行sql语句的返回类型为?
- Hibernate SQLQuery查询返回空List,在mysql命令下直接执行sql语句可以正常得到记录
- hibernate执行的SQL语句返回的list集合用Object[]接收遍历
- Hibernate执行原生SQL返回List<Map>类型结果集
- Hibernate使用sql语句查询,返回实体类型的方法及注意事项
- Hibernate 执行sql语句返回yntax error: syntax error, expect LPAREN, actual NOT not
- MyBatis中,传String类型参数到xml配置文件,并执行SQL语句后返回String类型的结果写法(jeesite)
- Hibernate执行原生SQL返回List<Map>类型结果集
- 关于用Hibernate执行sql查询,字符字段类型只返回第一个字节的问题
- Hibernate执行sql语句 返回可遍历list
- hibernate执行的SQL语句返回的list集合用Object[]接收遍历
- hibernate执行的SQL语句返回的list集合用Object[]接收遍历
- PDOstament对象执行execute()函数,只要是sql语句正确都是返回true
- 在Hibernate里执行Sql语句
- hibernate执行sql语句,转换为MAP
- oracle 存储过程执行可输入sql语句并返回结果集
- dbutils 执行sql返回的数据类型
- hibernate执行sql语句映射到实体类
- hibernate中的query查询原生sql语句,如何获得返回记录中的每一个字段的值
- 解决:mybatis执行SQL语句部分参数返回NULL