hibernate java.lang.Long cannot be cast to java.lang.Integer
2013-06-07 14:21
549 查看
从Hibernate 3.0.x/3.1.x升级到最新的3.2版之后,3.2版的很多sql函数如count(), sum()的唯一返回值已经从Integer变为Long,如果不升级代码,会得到一个ClassCastException。
这个变化主要是为了兼容JPA,可以在hibernate.org的最新文档中找到说明。
解决方案:1.hibernate尤其解决方案。当使用hibernate的查询函数count(),sum()等的值时(注意:一定是只返回唯一值的并且为数字格式是才可以)可已调用query的uniqueResult();方法 此方法返回Object对象,只需要把它转为Number类型,然后调用.intValue()即可。
例如:Integer count=((Number)(session.createQuery(hql)).uniqueResult()).intValue();
2.可以将long转为字符串,然后将字符串再转为integer;
Long i = (Long) session.createQuery(hql).uniqueResult();
Integer ii= new Integer(String.valueOf(i));
OK,这两种方案即可解决此类问题~
相关文章推荐
- hibernate:java.math.BigInteger cannot be cast to java.lang.Long
- hibernate :java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- hibernate :java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- java.lang.Long cannot be cast to java.lang.Integer的问题(Hibernate)
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- detachedCriteria: java.lang.integer cannot be cast to Long
- MyBatis 插入MySQL数据库返回主键异常 java.lang.Integer cannot be cast to java.lang.Long
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- java.lang.Long cannot be cast to java.lang.Integer解决办法
- java.math.BigInteger cannot be cast to java.lang.Long
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- java.math.BigInteger cannot be cast to java.lang.Long
- java.lang.Long cannot be cast to java.lang.Integer解决办法
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- HTTP Status 500 - java.lang.Long cannot be cast to java.lang.Integer
- java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
- java.lang.Long cannot be cast to java.lang.Integer解决办法
- java.math.BigInteger cannot be cast to java.lang.Long异常
- 报错:java.lang.Long cannot be cast to java.lang.Integer