40

I want to execute the below query in Hibernate?

select count(*) from login where emailid='something' and password='something'

1
  • Um... how about select count(*) from Login login where login.emailid='something' and login.password='something'? Or are you asking how to execute a HQL query at all? Jun 29, 2013 at 19:08

3 Answers 3

95

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.setParameter("email", "something");
        query.setParameter("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.

5
  • 1
    On using it the same way, I am getting an error, table or view does not exist. The query with the tablename, is working fine in db. Any idea what the error is?
    – Aparna
    Aug 16, 2016 at 7:03
  • Works fine with Hibernate 5.3.1.
    – BobC
    Jul 6, 2019 at 2:04
  • I had to use getSingleResult() instead uniqueResult(). The latter was not available
    – Davide
    Nov 13, 2020 at 10:07
  • 1
    If anyone like me faced the error of BigDecimal cannot cast it to Long when trying to use the solution above, use this: Long count = ((BigDecimal) query.uniqueResult()).longValue(); Dec 28, 2020 at 8:49
  • 1
    This query is not valid HQL and my no longer work if Hibernate will become more strict. It should be select count(login) from LoginClass login Mar 24, 2021 at 13:03
1

The result returns BigDecimal and you need to convert it from Bigdecimal (java.math.BigDecimal) to Long so that no error, he solution would be this:

Query query = session.createSQLQuery(
        "select count(*) from login where login.emailid=:email and login.password=:password");
query.setString("email", "something");
query.setString("password", "password");
Long count = ((BigDecimal) query.uniqueResult()).longValue();
3
  • 2
    This query is not valid HQL and my no longer work if Hibernate will become more strict. It should be select count(login) from LoginClass login Mar 24, 2021 at 13:04
  • no, it I did is with hibernate but native, it is already changed
    – Borja
    Mar 24, 2021 at 13:33
  • Ah ok. Thanks for the fix Mar 24, 2021 at 13:34
0

You can query in Hibernate 6 this way:

public int getCount() {
    int result = 0;
    try {
        session.beginTransaction();

        result = session.createNativeQuery("select count(*) from user", Integer.class).uniqueResult();

        session.getTransaction().commit();

    } catch (Exception exception) {
        exception.printStackTrace();
    }

    return result;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.