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. elasticsearch简介和倒排序索引介绍

    介绍 我们为什么要用搜索引擎?我们的所有数据在数据库里面都有,而且 Oracle.SQL Server 等数据库里也能提供查询检索或者聚类分析功能,直接通过数据库查询不就可以了吗?确实,我们大部分的查 ...

  2. 我的IT之路2013(二)

    严寒即将过去,温暖的春天正在向我们招手,欢呼吧,在迎接新的开始的同时,不要忘了回顾一下过去的这一年,总结一下过去的这一年有什么得失. 英语学习 13年下半年,最大的变化就是有很大一部分时间用来学英语. ...

  3. git报错之index.lock

    当想回退到某个版本的时候,用git reset --hard commit_id,发现报错,原因是.git目录下多了个index.lock文件,可以通过rm命令删除,然后再回退 rm -f ./.gi ...

  4. 最全Java锁详解:独享锁/共享锁+公平锁/非公平锁+乐观锁/悲观锁

    在Java并发场景中,会涉及到各种各样的锁如公平锁,乐观锁,悲观锁等等,这篇文章介绍各种锁的分类: 公平锁/非公平锁 可重入锁 独享锁/共享锁 乐观锁/悲观锁 分段锁 自旋锁 01.乐观锁 vs 悲观 ...

  5. [Windows Azure] Getting Started with Windows Azure SQL Database

    In this tutorial you will learn the fundamentals of Windows Azure SQL Database administration using ...

  6. Python之Flask和Django框架解决跨域问题,配合附加ajax和fetch等js代码

    Flask框架py解决跨域问题示例: # -*- coding: utf- -*- # by zhenghai.zhang from flask import Flask, render_templa ...

  7. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  8. whatweb.rb 未完待续

    #!/usr/bin/env ruby #表示ruby的执行环境 =begin # ruby中用=begin来表示注释的开始 .$$$ $. .$$$ $. $$$$ $$. .$$$ $$$ .$$ ...

  9. bootstrap table 自己设置值

    在使用bootstrap table的值默认是从 total rows 里面取,可是后台返回的json数据并不是这样的怎么办 可以使用 responseHandler  自定义: $(function ...

  10. 【转】如何使用visual studio将你的程序打包成安装包

    原文地址:https://www.cnblogs.com/SolarWings/p/6132310.html 很久很久以前,我一直有一个梦想,那就是做出一个自己的游戏,这个游戏很像模像样,除了拥有一个 ...