`
soongbo
  • 浏览: 89305 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

http://www.itpub.net/thread-975781-1-1.html

阅读更多

通过Hibernate进行字段中的数据计算,在JDBC中,就使用SQL的sum等方法,而在使用Hibernate中,需要使用Projections的对象.

首先介绍一下Projections:
org.hibernate.criterion.Projections是 Projection 的实例工厂,我们通过调用setProjection()应用Projections对象进行一个查询.

下面开始操作:
同样需要创建Criteria对象,这对象非常重要,几乎完成了Hibernate中大多的查询功能,配合HQL使用,相应厉害.
Java代码
Criteria criteria = session.createCriteria(AccCasherDaySupply.class);  

Criteria criteria = session.createCriteria(AccCasherDaySupply.class);

通过Session创建Criteria对象,参数为你要查询的对象类名(这里注意区分大小写).

通过Criteria对象可以使用setProjection的方法,该方法将进行一个Projections的查询.
Java代码
criteria.setProjection(****);  

criteria.setProjection(****);


接下来就是进行真正的聚合操作部分了,使用Projections对象插入到****的参数中.
Projects对象具有大多聚合的方法,基本上有:rowCount(),avg(),max(),groupProperty(),alias(),property(),sum()...
以sum()为例,如果需要计算某个字段数据的合,将使用sum()方法:
Java代码
criteria.setProjection(Projections.sum("字段名"));  

criteria.setProjection(Projections.sum("字段名"));


通过以上代码,criteria将得到相应字段名的合.
最后通过criteria.list()得到Projections查询的最终结果.

分享到:
评论

相关推荐

    PowerDesigner 12汉化+破解补丁

    http://ms.itpub.net/thread-765553-1-1.html 3. 运行PowerDesigner 12程序,点击Tools -> License Parameters -> Standalone Seat - Local License,导入PowerDesigner 12压缩包(即第一步中下载的安装压缩包)中...

    收集的部分软件测试网址

    4. **ITPUB(http://www.itpub.net/thread-829756-1-1.html)** - **知识点**:ITPUB是一个技术交流平台,用户可以在这里提问、解答关于软件测试的问题,获取实践经验。 5. **测试联盟...

    android学习资料(网站,论坛)

    1. **ITPub博客(http://publish.itpub.net/lists/11554/0/11554_1.shtml)**:汇集了多位资深开发者的个人博客,涉及Android开发的多个方面。 2. **西里汉林博客(http://www.cnblogs.com/xirihanlin/)**:作者...

    类似MSSQL的查询分析器

    3. "httpwww.itpub.netthread-1208656-5-1.html.txt" - 这看起来像是一个从网页上复制的URL,原始形式可能是"http://www.itpub.net/thread-1208656-5-1.html",这可能指向一个IT论坛的帖子,讨论了这个查询分析器的...

    IBM WebSphere Red Book6

    - [ITPub论坛讨论](http://www.itpub.net/viewthread.php?tid=1234255) - [IBM DeveloperWorks论坛讨论](http://www.ibm.com/developerworks/forums/thread.jspa?threadID=85080) - [IBM RedBooks官方文档]...

Global site tag (gtag.js) - Google Analytics