博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate使用count(*)取得表中记录总数
阅读量:4069 次
发布时间:2019-05-25

本文共 2134 字,大约阅读时间需要 7 分钟。

Java代码  
  1. /** 
  2.   * @TODO:查询某一年度的所有计划数量 
  3.   */  
  4. public int findCountByYear(String currYear) {  
  5.     String hqlString = "select count(*) from WaterPlan as p where p.planYear ='"+currYear+"'";  
  6.     Query query = this.getSession().createQuery(hqlString);  
  7.           
  8.     return ((Number)query.uniqueResult()).uniqueResult();  
  9. }  
/**  * @TODO:查询某一年度的所有计划数量  */public int findCountByYear(String currYear) {    String hqlString = "select count(*) from WaterPlan as p where p.planYear ='"+currYear+"'";    Query query = this.getSession().createQuery(hqlString);		    return ((Number)query.uniqueResult()).uniqueResult();}

 

 从Hibernate 3.0.x/3.1.x升级到最新的3.2版之后,3.2版的很多sql函数如count(), sum()的唯一返回值已经从Integer变为Long,如果不升级代码,会得到一个ClassCastException。

这个变化主要是为了兼容JPA,可以在hibernate.org的最新文档中找到说明。
Hibernate Team也提供了一个与原来兼容的解决方案:

Java代码  
  1. Configuration classicCfg = new Configuration();   
  2. classicCfg.addSqlFunction( "count"new ClassicCountFunction());   
  3. classicCfg.addSqlFunction( "avg"new ClassicAvgFunction());   
  4. classicCfg.addSqlFunction( "sum"new ClassicSumFunction());   
  5. SessionFactory classicSf = classicCfg.buildSessionFactory();   
Configuration classicCfg = new Configuration(); classicCfg.addSqlFunction( "count", new ClassicCountFunction()); classicCfg.addSqlFunction( "avg", new ClassicAvgFunction()); classicCfg.addSqlFunction( "sum", new ClassicSumFunction()); SessionFactory classicSf = classicCfg.buildSessionFactory();

 

 

或 

Java代码  
  1. //int count = ((Integer)query.uniqueResult()).intValue();   
  2. //改成   
  3.   
  4. int count = ((Number)query.uniqueResult()).intValue();   
  5.   
  6. //这样就可以两个版本同时兼容.   
//int count = ((Integer)query.uniqueResult()).intValue(); //改成 int count = ((Number)query.uniqueResult()).intValue(); //这样就可以两个版本同时兼容.

 

  

Java代码  
  1. //参考代码  
  2. //第一种方法:  
  3.   String hql = "select count(*) from User as user";  
  4.   Integer count = (Integer)getHibernateTemplate().find(hql).listIterator().next();  
  5.   return count.intValue();  
  6.   
  7. //第二种方法:  
  8.  String hql = "select count(*) from User as user";  
  9.   return ((Integer)getHibernateTemplate().iterate(hql).next()).intValue();  
  10.   
  11. //第三种方法:  
  12.  String hql = "select count(*) from User as user";  
  13.  Query query =  getHibernateTemplate().createQuery( getSession(),hql);  
  14.  return ((Integer)query.uniqueResult()).intValue();   

 

转载地址:http://vrmji.baihongyu.com/

你可能感兴趣的文章
2006年中国软件收入规模前100家企业名单
查看>>
给毕业生的忠告
查看>>
2006年培养员工的失败与成功之处
查看>>
管理出效益
查看>>
感谢工程二部的所有员工!!!
查看>>
MINIGUI学习笔记之安装使用
查看>>
MINIGUI与共享内存
查看>>
浮点型(FLOAT)与CHAR型转换
查看>>
何为团队?
查看>>
如何做好一个项目经理?
查看>>
寻求工控、自动化合作者建立工控网站!(长期有效)
查看>>
MiniGUI 2.0.3商业版本安装
查看>>
REDHAT 网络通讯源码
查看>>
LINUX 串口通讯源码
查看>>
浙西2日游(4.21-22)
查看>>
LINUX起死回生记
查看>>
网站规划之初稿 (工控帮 http://www.opc-china.com)
查看>>
网站系统之咨询(网奇行业门户)(如此做法怎么能对的起“中国建站第一品牌”)
查看>>
数字电影《大寒小寒》(淳朴,无私)
查看>>
讨论:国内企业对开发的投入是多少
查看>>