Hibernate用一些标准对行进行计数 [英] Hibernate count rows with some criterias

查看:153
本文介绍了Hibernate用一些标准对行进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Person Person,并且我想要统计所有那些birthDate不为null且他们是学生的人。
假设我有两列:

  birthDate Date(可以为null)
isStudent布尔值(默认值:假)

我如何使用hibernate来做到这一点?

解决方案

  Criteria crit = session.createCriteria(Person.class); 
crit.add(Restrictions.isNotNull(birthDate));
crit.add(Restrictions.eq(isStudent,true));
crit.setProjection(Projections.rowCount());
Integer count =(Integer)crit.uniqueResult();


Suppose I have a table Person and i want to count all those people whose "birthDate" is not null and they are a student. Assuming i have two columns :

birthDate Date (can be null)
isStudent boolean (default: false)

How can i do this using hibernate.. ?

解决方案

Criteria crit = session.createCriteria(Person.class);
crit.add( Restrictions.isNotNull("birthDate"));
crit.add( Restrictions.eq("isStudent", true));
crit.setProjection(Projections.rowCount());
Integer count = (Integer)crit.uniqueResult();

这篇关于Hibernate用一些标准对行进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆