如何在hibernate中为count

如何在hibernate中为count

本文介绍了如何在hibernate中为count(*)编写查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Hibernate 中执行下面的查询?



假设您的登录表由 LoginClass 类,使用 emailid 和 password 实例变量。然后执行如下所示的操作:

  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();

它应该返回 count 正在寻找。您只需将名称改为您的班级和参数名称即可。


I want to execute the below query in Hibernate?

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

解决方案

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.

这篇关于如何在hibernate中为count(*)编写查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:09