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的更多相关文章

  1. 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 ...

  2. java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

    最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...

  3. 关于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 ...

  4. 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 ...

  5. 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 ...

  6. 安卓出现错误: 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 ...

  7. java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView

    今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...

  8. java.lang.ClassCastException: android.app.Application cannot be cast to

    出这个异常的原因是在项目中添加了新lication类(public class Application extends lication)之后,没有在AndroidManifest.xml中添加该类的 ...

  9. 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 ...

  10. java.lang.ClassCastException: sun.proxy.$Proxy11 cannot be cast to分析

    报这个错,只有一个原因,就是你转化的类型不对. 如果你的类是一个单实体类,也就是没有继承或是接口别的类. public class HjmServiceImpl {} 那么这样写就可以: HjmSer ...

随机推荐

  1. 自动化运维工具SaltStack详细部署

    ==========================================================================================一.基础介绍==== ...

  2. C++中的友元函数和友元类

    C++中的友元函数主要应用于以下场景: 友元函数 第一种场景 代码中有一个全局函数,该函数想要去访问某个类的成员变量(该类的成员变量是private的,且该类并未提供任何获取获取私有成员变量的publ ...

  3. hi模板文件报乱码问题

    1.h5模板在: Templates\common 2.出现乱码的都在Templates\common\tags  局部视图里. 3.页面顶部有引用(skin-Common_SubmmitCartPr ...

  4. sql自动创建表并复制数据

    ---------------自动创建表并复制数据sql,需要自己设置主键----------- select * into 新表 from 旧表

  5. 歌曲播放页面的数据vuex管理

    1.state.js import {playMode} from '@/common/js/config' const state = { singer:{}, playing:false, ful ...

  6. 【Bootstrap Method】Evaluating The Accuracy of a Classifier

    自助法介绍: 非参数统计中一种重要的估计统计量方差进而进行区间估计的统计方法,也称为自助法.其核心思想和基本步骤如下:(1)采用重抽样技术从原始样本中抽取一定数量(自己给定)的样本,此过程允许重复抽样 ...

  7. Delphi中的三目运算函数有哪些?(XE10.2+WIN764)

    相关资料:https://www.cnblogs.com/rogge7/p/6078903.html 问题现象:在做一个判断时突然想到了C++的三目运算,就在想Delphi中一共有几个? 问题处理: ...

  8. java工具类POI导出word

    1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...

  9. virtualbox 安装 USB 扩展功能

    virtualbox USB 扩展包下载 扩展包下载地址: http://download.virtualbox.org/virtualbox/ 选择你的 virtualbox 版本 看版本在 vir ...

  10. redis主从配置(docker实现)

    一.docker新建两个redis服务端,并分别设置端口为6379和6380 命令如下: docker run -p : -d --name redis-server docker.io/redis: ...