How do we count rows using older versions of Hibernate (~2009)?

JavaHibernateCount

Java Problem Overview


For example, if we have a table Books, how would we count total number of book records with hibernate?

Java Solutions


Solution 1 - Java

For older versions of Hibernate (<5.2):

Assuming the class name is Book:

return (Number) session.createCriteria("Book")
                  .setProjection(Projections.rowCount())
                  .uniqueResult();

It is at least a Number, most likely a Long.

Solution 2 - Java

In Java i usually need to return int and use this form:

int count = ((Long)getSession().createQuery("select count(*) from Book").uniqueResult()).intValue();

Solution 3 - Java

Here is what official hibernate docs tell us about this:

You can count the number of query results without returning them:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()

However, it doesn't always return Integer instance, so it is better to use java.lang.Number for safety.

Solution 4 - Java

You could try count(*)

Integer count = (Integer) session.createQuery("select count(*) from Books").uniqueResult();

Where Books is the name off the class - not the table in the database.

Solution 5 - Java

Long count = (Long) session.createQuery("select count(*) from  Book").uniqueResult();

Solution 6 - Java

If you are using Hibernate 5+, then query will be modified as

Long count = session.createQuery("select count(1) from  Book")
                    .getSingleResult();

Or if you Need TypedQuery

Long count = session.createQuery("select count(1) from  Book",Long.class)
                        .getSingleResult();
                

Solution 7 - Java

This works in Hibernate 4(Tested).

String hql="select count(*) from  Book";
Query query= getCurrentSession().createQuery(hql);
Long count=(Long) query.uniqueResult();
return count;

Where getCurrentSession() is:

@Autowired
private SessionFactory sessionFactory;


private Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}

Solution 8 - Java

It's very easy, just run the following JPQL query:

int count = (
(Number)
	entityManager
	.createQuery(
		"select count(b) " +
		"from Book b")
	.getSingleResult()
).intValue();

The reason we are casting to Number is that some databases will return Long while others will return BigInteger, so for portability sake you are better off casting to a Number and getting an int or a long, depending on how many rows you are expecting to be counted.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncraftsmanView Question on Stackoverflow
Solution 1 - JavaSalandurView Answer on Stackoverflow
Solution 2 - JavamariooshView Answer on Stackoverflow
Solution 3 - JavaAntonioView Answer on Stackoverflow
Solution 4 - JavaJon SpokesView Answer on Stackoverflow
Solution 5 - JavaxrcwrnView Answer on Stackoverflow
Solution 6 - JavarajadilipkolliView Answer on Stackoverflow
Solution 7 - JavaLucianoDemuruView Answer on Stackoverflow
Solution 8 - JavaVlad MihalceaView Answer on Stackoverflow