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

我的梦境 提交于 2019-12-03 08:22:00

问题


I want to execute the below query in Hibernate?

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


回答1:


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.




回答2:


other solution may be createSQLQuery("SQL STATEMENT") if you Compelled to good luck



来源:https://stackoverflow.com/questions/17383697/how-to-write-a-query-in-hibernate-for-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!