Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

hibernate

hql

I want to execute the below query in Hibernate?

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

like image 287
user2510115 Avatar asked Jun 29 '13 18:06

user2510115


People also ask

What is the description for count () in hibernate?

Hibernate Query Language (HQL) supports the following aggregate functions in SELECT statements. Except count() , all other functions except numeric values as arguments. The count() can be used to count any kind of values, including the number of rows in the query result.

Can we write SQL query in hibernate?

You can use native SQL to express database queries if you want to utilize database-specific features such as query hints or the CONNECT keyword in Oracle. Hibernate 3. x allows you to specify handwritten SQL, including stored procedures, for all create, update, delete, and load operations.

What is query uniqueResult () in hibernate?

uniqueResult. Convenience method to return a single instance that matches the query, or null if the query returns no results.


1 Answers

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.

like image 144
eternay Avatar answered Sep 22 '22 09:09

eternay