Hibernate count和sum查询
来源:互联网 发布:碧之轨迹优化破解补丁 编辑:程序博客网 时间:2024/05/17 18:47
前些日子用到了Hibernate,本人菜鸟一个,刚接触Hibernate,想做一个count和sum一起的查询,结果卡了老半天,网上百度也没找到结果,后来迫不得已FQ找,最终找到了答案。其实这在Hibernate的API文档里写到过,当时没注意,我是后来FQ时才发现的。所以还是得靠文档。最开始我相信做简单的count(*) 或是sum查询没问题,只需要返回((Long)session.createQuery(hql).uniqueResult()).intValue()即可,但是如果要返回多个结果的话,而且又不是一个对象的属性,那就要返回一个List或是Map类型的对象了。例如使用map的话hql语句中
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )from Cat cat
使用list:
select new list(mother, offspr, mate.name)from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr
Map<String,Object> map =(Map) SessionHelper.getSession().createQuery(hql).uniqueResult();
就能得到一个map对象了,之后就可以通过key的值取出你想要的字段了。我还没想到更好的办法,因为刚接触Hibernate多久,所以大家有更好的办法欢迎指出。
0 0
- Hibernate count和sum查询
- hibernate count(*) 查询
- hibernate:count查询
- Hibernate 使用 count(*)、avg、sum
- Sql中的sum()和count()
- mysql count(),sum()等作为条件查询
- mysql count(),sum()等作为条件查询
- hibernate QBC count查询(投影)
- hibernate中关于对count(*)的查询
- sql中sum()和count()的区别
- sum函数和count函数连用
- oracle 中sum和count的区别
- SQL Server COUNT() 和SUM()的使用方法
- count(*)和sum(1) 的效率
- SQL Server COUNT() 和SUM()的使用方法
- sum()over()和count()over()分析函数
- oracle 中sum和count的区别
- sql中sum()和count()的区别
- php让你头疼的浮点数运算
- 你從來不缺智慧,缺的是......
- Image Lazy Load:那些延时加载图片的开源插件(jQuery)
- git push -f
- String转化为byte[]和byte[]转化为String
- Hibernate count和sum查询
- JDBC连接池的testQuery/validationQuery设置
- vmlinux
- android高德地图用invisiable和visiable切换时,出现闪烁的解决方案
- PHP版本如何选择?应该使用哪个版本?
- 【实例】模拟Spring
- RSA算法原理(二)
- 用jQuery与JSONP轻松解决跨域访问的问题
- MySQL 5.5 以上 time,date,date time使用like,Illegal mix of collations for operation 'like'