Hibernate 执行SQL语句的count函数返回BigDecimal类型

本文探讨了使用Hibernate在不同数据库环境下进行计数查询时返回类型的差异。包括使用HibernateTemplate执行SQL和HQL时返回的类型区别,以及Criteria API的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     当前使用的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类型。

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());
		}


假设我们有一个 Product 表,其中包含 id、name 和 price 字段,现在需要修改其中的一条数据的价格,可以使用以下示例 SQL 语句: ``` UPDATE Product SET price = 99.99 WHERE id = 1; ``` 该语句将 Product 表中 id 为 1 的记录的 price 修改为 99.99。其中,UPDATE 用于修改数据,SET 用于指定需要修改的字段及其值,WHERE 用于指定需要修改的记录,这里指定 id = 1 的记录。在实际开发中,需要根据具体情况进行修改字段和记录的指定。 由于 BigDecimal 类型的数据需要精确控制小数位数,因此在执行 SQL 语句时需要使用 PreparedStatement 对象,并且使用 setBigDecimal 方法设置参数。这里给出一个使用 PreparedStatement 对象执行 BigDecimal 类型修改的 SQL 语句的示例: ```java public void updateProductPrice(BigDecimal price, int id) { String sql = "UPDATE Product SET price = ? WHERE id = ?"; try (PreparedStatement ps = connection.prepareStatement(sql)) { ps.setBigDecimal(1, price); ps.setInt(2, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } ``` 该方法接收一个 BigDecimal 类型的 price 和一个 int 类型的 id 作为参数。在方法中,使用 PreparedStatement 对象执行 SQL 语句,使用占位符 '?' 代替 SQL 语句中的具体值,最后使用 setBigDecimal 方法将 BigDecimal 类型的 price 设置为占位符中的第一个参数,将 int 类型的 id 设置为占位符中的第二个参数。最后执行更新操作,修改 Product 表中 id 等于指定值的记录的价格为指定的 BigDecimal 类型的值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值