java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
hibernate查询结果条数集
原写法:
Integer count = (Integer )session.createQuery(hql).uniqueResult();
报错:java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
原因:
从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));
这两种方案即可解决此类问题~
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer的更多相关文章
- java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
http://stackoverflow.com/questions/13269564/java-lang-classcastexception-oracle-sql-timestamp-cannot ...
- java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...
- 关于android使用ksoap2报Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast to org.ksoap2.serializa ...
- java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
错误现象: [framework] 2016-05-26 11:34:53,590 -INFO [http-bio-8080-exec-7] -1231863 -com.dhcc.base.db.D ...
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView
今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...
- java.lang.ClassCastException: android.app.Application cannot be cast to
出这个异常的原因是在项目中添加了新lication类(public class Application extends lication)之后,没有在AndroidManifest.xml中添加该类的 ...
- Android: java.lang.ClassCastException: android.widget.imageView cannot be cast to android.widget.textView异常解决
有时在修改xml文件时,全报这种错误,这个应该是缓存没得到及时更新导致的,可以通过以下方法解决: Eclipse tends to mess up your resources every now a ...
- java.lang.ClassCastException: sun.proxy.$Proxy11 cannot be cast to分析
报这个错,只有一个原因,就是你转化的类型不对. 如果你的类是一个单实体类,也就是没有继承或是接口别的类. public class HjmServiceImpl {} 那么这样写就可以: HjmSer ...
随机推荐
- Shell脚本判断内容为None的方式
1.判断变量 read -p "input a word :" word if [ ! -n "$word" ] ;then echo "you ha ...
- HBase写入性能改造(续)--MemStore、flush、compact参数调优及压缩卡的使用【转】
首先续上篇测试: 经过上一篇文章中对代码及参数的修改,Hbase的写入性能在不开Hlog的情况下从3~4万提高到了11万左右. 本篇主要介绍参数调整的方法,在HDFS上加上压缩卡,最后能达到的写入 ...
- HBase操作(Shell与Java API)
版权声明:本文为博主原创文章,未经博主允许不得转载. 转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...
- 【Mysql】linux连接mysql错误解决方案
1.源码 //connect-mysql.c #include <stdio.h> #include "/usr/include/mysql/mysql.h" int ...
- Netty高性能之Reactor线程模型
Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...
- WebService 获取当前URL
private string pageRoot = string.Empty; public virtual string PortalRoot { get { if (pageRoot == nul ...
- error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
备注:我上次遇到这个问题是Win32 DLL项目中无意中include了afxwin.h,这个是MFC的头文件,把这个include删掉就解决了 ================ 转自:http:// ...
- Zabbix监控数据库连通性所遇问题
Zabbix配合db2bp监控DB2数据库能否远程连接问题分析: 所遇问题,有时监控一直获取不到数据,原因是connect to连接超时了,zabbix默认监控脚本获取数据时间是3s,但最多支持30s ...
- zip伪加密
简单的话来阐述 zip伪协议的意思是说本来不需要密码的zip文件然后通过修改标志位,然后就可以达到有密码的效果对吗?但是他实际是没有密码. 一个 ZIP 文件由三个部分组成: 压缩源文件数据区+压缩源 ...
- thinkphp验证码乱码的解决办法
很有可能是入口文件index.php和.htaccess文件要转换成 以UTF-8无BOM格式编码