-1

I am trying to run a simple SQL(native) query within a hibernate application.

SELECT COUNT(*) from users

The answer to the select may be saved into a integer. How can i do this without having to create a model class in Hibernate?

AlexanderH
  • 11
  • 2

3 Answers3

0
int count=0;    
Resultset rs=Statement.ExecuteQuery("SELECT COUNT(*) as count from users");
    if(rs.next()){
     count=rs.getInt("count");
    }

System.out.println(count);
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
0

Hibernate 5.0.9.Final will give that result as a BigInteger type object. Assuming you have an EntityManager, then use:

BigInteger r = (BigInteger) em.createNativeQuery("select count(*) from user").getSingleResult();
System.out.println(r);

This is a simple question and you should be able to find the answer easily on stackoverflow:

hibernate native query, count [duplicate].

How do we count rows using Hibernate?

Community
  • 1
  • 1
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
-1
Resultset rs=Statement.ExecuteQuery("SELECT COUNT(*) as number from users");
while(rs.next()){
 Long count =rs.getLong("number");
    }
Rom
  • 67
  • 1
  • 2
  • 12