I want to execute the below query in Hibernate?
select count(*) from login where emailid='something' and password='something'
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.
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.
uniqueResult. Convenience method to return a single instance that matches the query, or null if the query returns no results.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With