Karan Gujral Karan Gujral - 1 year ago 94
Java Question

Hibernate count rows with some criterias

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.. ?

Answer Source
Criteria crit = session.createCriteria(Person.class);
crit.add( Restrictions.isNotNull("birthDate"));
crit.add( Restrictions.eq("isStudent", true));
Integer count = (Integer)crit.uniqueResult();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download