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. fpm制做mysql-5.6.33 rpm包

    增加用户: # groupadd -r mysql # useradd -g mysql -r -s /sbin/nologin -M -d /data/my_db mysql 源码安装mysql-5 ...

  2. dom4j: 生成XML时文本中回车换行无效

    属性文本中回车换行在输出时被dom4j自动去掉了. 解决办法: 将format.setTrimText(false); 即可.因为 createPrettyPrint()方法中有 format.set ...

  3. DataTable的初始化与事件注册

    TypeScript代码: let tabledata = data as any[]; if (this.dataTable) { this.dataTable.clear(); this.data ...

  4. NET Core 1.1 版本项目和2.0环境下的项目开发注意事项

    在NET Core 1.1开发下的项目最好不要随便把工具更新升级到2.0,这样最容易导致之前的.NETCore直接被升级不兼容早前版本 会引起项目无法启动在运行调试IIS express 时候直接一闪 ...

  5. webpack配置提取公共代码

    公共代码提取功能是针对多入口文件的: 背景:在pageA.js和pageB.js中分别引用subPageA.js和subPageB.js webpack.config.js文件: var path = ...

  6. linux centOS6 nexus 开启自动启动

    sudo ln -s /opt/nexus-2.6.4/nexus-2.6.4-02/bin/nexus /etc/init.d/nexus 使用  service nexus status/star ...

  7. 在windows下nginx+django+flup python3

    1.安装python 下载最新的python版本,在本文撰写时为 python 3.4, 下载地址:https://www.python.org/ftp/python/3.4.0/python-3.4 ...

  8. jquery文字纵向滚动效果(带间隔停留)

    <script type="text/javascript"> //文字纵向滚动 $(function() { var $this = $("#quotati ...

  9. mysqldump具体应用实例

    1.导出整个数据库  mysqldump -h主机 -u 用户名 -p 数据库名 > 导出的文件名      mysqldump -h127.0.0.1 -u wcnc -p smgp_apps ...

  10. (转载)WPF中的动画——(一)基本概念

    http://www.cnblogs.com/TianFang/p/4050845.html WPF的一个特点就是支持动画,我们可以非常容易的实现漂亮大方的界面.首先,我们来复习一下动画的基本概念.计 ...