How to Count the Rows in Hibernate Query Language

How to Count the rows in Hibernate Query Language?

Have you tried the query.uniqueResult(); ? As your Select count(*) will give you only one number, you should be able to retrieve it with this like int count = (Integer)query.uniqueResult();

To count based on a Criteria you can do this:

Criteria criteria = currentSession().createCriteria(type);

I'm using the Criteria right now so I know for sure that it works. I saw the uniqueResult() solution on a website here:

How to write a query in hibernate for count(*)

Suppose your login table is mapped by a LoginClass class, with emailid and password instance variables. Then you'll execute something like:

Query query = session.createQuery(
"select count(*) from LoginClass login where login.emailid=:email and login.password=:password");
query.setString("email", "something");
query.setString("password", "password");
Long count = (Long)query.uniqueResult();

It should return in count the result you're looking for. You just have to adapt the name to your class and your parameter names.

Hibernate count rows with some criterias

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

Word count in Hibernate Query Language (hql)

I don't know of a way to do this using HQL alone. As a workaround, you can read each row into Java, and then manually count the number of words in details.

If you are open to using a native query, then we can try counting words by counting the number of spaces in the details column. For example, on MySQL and Oracle, you could try the following query:

FROM post
WHERE LENGTH(details) - LENGTH(REPLACE(details, ' ', '')) > 98;

SQL Count in Hibernate HQL

I don't have a hibernate setup available to test, but it seems like you can:

How to count row table in JPA Query

According repositories documentation using CrudRepository provides a method called count() that is one of the Superinterfaces which JpaRepository is implemented.

Based CrudRepository documentation says:

long count(); Returns the number of entities.

Then you should use CrudRepository method. In addition Remove Uppercase MembershipREPOSITORY, by java code convention, you have to use by following way MembershipRepository.

public interface MembershipRepository extends JpaRepository <ActIdMembershipEntity, String> {

And use it in your Service:

public class MembershipService {

private MembershipRepository repo;

public long count() {
return repo.count();


Based on your requirement:
In Controller:

public class MembershipResource {

private MembershipService membershipService;

public List<Object> list() { return membershipService.countMemberships();

In Service:

public class MembershipService {

private MemershipRepository repository;

public List<Object> countMemberships() {
return repository.countMemberships();


In Repository:

public interface MemershipRepository extends JpaRepository<ActIdMembershipEntity, String> {
@Query ("select i.userId, count(i) from ActIdMembershipEntity i where i.userId ='kermit'")
List<Object> countMemberships();

Related Topics

Leave a reply