[英]How to Count the rows in Hibernate Query Language?
I am trying to get just the count of the rows returned rather than all the results from the table. 我正在尝试只获取返回的行数,而不是表中的所有结果。
I saw that this can be done like this: 我看到可以这样做:
( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()
But when trying to store this query in an integer format(it says cannot convert from Query to Integer
) 但是当尝试以整数格式存储此查询时(表示无法从
Query to Integer
转换Query to Integer
)
I am using a dynamic query where the values will be mentioned below the query like this 我正在使用动态查询,其中的值将像这样在查询下方提及
theQuery = "select count(*) from THM as thm " +
"join thm.TMC as tmc " +
"join tmc.TIMCC as timcc " +
"where thm.Qid = :Qid and thm.Cv = :Cv and timcc.Did = :Did and timcc.Cv= :Cv";
Query query = session.createQuery(theQuery);
query.setInteger("Qid", Integer.parseInt(Qid));
query.setInteger("Did", Did);
query.setInteger("Cv",cV);
Now, how can i get a count of all the rows returned by using Hibernate query in a variable without using list.size
but directly from the query? 现在,如何在不使用
list.size
但直接从查询中使用变量的情况下,通过使用Hibernate查询返回的所有行的计数?
Have you tried the query.uniqueResult(); 您是否尝试过query.uniqueResult(); ?
? As your Select count(*) will give you only one number, you should be able to retrieve it with this like int count = (Integer)query.uniqueResult();
由于您的Select count(*)仅会为您提供一个数字,因此您应该可以使用int count =(Integer)query.uniqueResult();来检索它。
To count based on a Criteria you can do this: 要根据条件进行计数,您可以执行以下操作:
Criteria criteria = currentSession().createCriteria(type);
criteria.setProjection(Projections.rowCount());
criteria.uniqueResult();
I'm using the Criteria right now so I know for sure that it works. 我现在正在使用条件,因此我确定它可以正常工作。 I saw the uniqueResult() solution on a website here: http://www.jroller.com/RickHigh/entry/hibernate_pagination_jsf_datagrid_prototype1
我在以下网站上看到了uniqueResult()解决方案: http ://www.jroller.com/RickHigh/entry/hibernate_pagination_jsf_datagrid_prototype1
你可以这样做
long count = (long)session.createQuery("SELECT COUNT(e) FROM Employees e").getSingleResult();
Try it. 试试吧。
Long count = ((Long) session.createQuery("select count(*) from Book").uniqueResult());
Integer totalBooks = count.intValue();
Work for me 为我工作
int list=(int)
sessionFactory.getCurrentSession().createSQLQuery("select count(*) as count from
Book").addScalar("count",IntegerType.INSTANCE).uniqueResult();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.