Hibernate HQL:获取结果的数量而不实际返回它们 [英] Hibernate HQL: Get count of results without actually returning them

查看:228
本文介绍了Hibernate HQL:获取结果的数量而不实际返回它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得动态生成的HQL查询结果的计数,但实际上并没有得到结果列表。说我的查询是这样的:

  select公司LEFT OUTER JOIN FETCH产品产品

我在Hibernate文档中读到:


<您可以计算查询结果的数量而不返回它们:



 ((Integer) session.createQuery(select count(*)from ....)。iterate()。next()).intValue()

我怀疑我应该用我的查询替换 .... ,但这不起作用,因为HQL不支持FROM中的子选择。 / p>

那么,我该如何计算动态生成的HQL查询的结果呢?我认为通过执行它并获得结果列表的.size()可能是不必要的开销。



干杯!



**更新:**



我用这个正则表达式转换我的查询:

  Number num =(Number)em.createQuery(dynamicQuery.replaceAll(select \\w + from,select count(*)from))。getSingleResult(); 

我得到这个:


Blockquote



EJB异常:嵌套的异常是:java.lang.IllegalArgumentException:org.hibernate.QueryException:查询指定的连接读取,但获取的关联的所有者不在选择列表中[FromElement {显式,不是集合连接,fetch连接,fetch非连接-lazy属性,classAlias = product,role = org.myCompany.applicant.entity.Applicant.products,tableName = PRS_DEV.PRODUCT,tableAlias = products1_,origin = PRS_DEV.APPLICANT applicant0_,colums = {applicant0_.APPLICANT_ID,className = org。 myCompany.product.entity.Product}}] [从org.myCompany.applicant.entity.Applicant申请人选择count()申请人LEFT OUTER JOIN FETCH申请人。产品];嵌套的异常是:java.lang.IllegalArgumentException:org.hibernate.QueryException:查询指定的连接读取,但获取的关联的所有者不在选择列表中[FromElement {显式,不是集合连接,fetch连接,fetch非连接-lazy属性,classAlias = product,role = org.myCompany.applicant.entity.Applicant.products,tableName = PRS_DEV.PRODUCT,tableAlias = products1_,origin = PRS_DEV.APPLICANT applicant0_,colums = {applicant0_.APPLICANT_ID,className = org。 myCompany.product.entity.Product}}] [select count()from org.myCompany.applicant.entity.Applicant申请人LEFT OUTER JOIN FETCH申请人。产品产品]


解决方案

这应该会有诀窍:

  select count(*)FROM公司c JOIN ... 

没有子选择,而不是返回公司,您返回计数。



编辑: FETCH

  org.myCompany.applicant.entity.Applicant应用程序中的select count(产品)
LEFT OUTER JOIN申请人。产品产品


I want to get the count of the results of a dynamically-generated HQL query, without actually getting the list of results. Say that the query I have is something like:

select Company company LEFT OUTER JOIN FETCH products product

I read in the Hibernate documentation that:

You can count the number of query results without returning them:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()

I suspected that I should be replacing the .... with my query, but that does not work, as HQL does not support sub-selects in FROM.

So, how should I count the results of a dynamically generated HQL query? I think that by executing it and getting the .size() of the results list may be unnecessary overhead.

Cheers!

**UPDATE: **

I used this regex to convert my query:

Number num = (Number) em.createQuery(dynamicQuery.replaceAll("select \\w+ from ", "select count(*) from ")).getSingleResult();

And I get this:

Blockquote

EJB Exception: ; nested exception is: java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=product,role=org.myCompany.applicant.entity.Applicant.products,tableName=PRS_DEV.PRODUCT,tableAlias=products1_,origin=PRS_DEV.APPLICANT applicant0_,colums={applicant0_.APPLICANT_ID ,className=org.myCompany.product.entity.Product}}] [select count() from org.myCompany.applicant.entity.Applicant applicant LEFT OUTER JOIN FETCH applicant.products product ]; nested exception is: java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=product,role=org.myCompany.applicant.entity.Applicant.products,tableName=PRS_DEV.PRODUCT,tableAlias=products1_,origin=PRS_DEV.APPLICANT applicant0_,colums={applicant0_.APPLICANT_ID ,className=org.myCompany.product.entity.Product}}] [select count() from org.myCompany.applicant.entity.Applicant applicant LEFT OUTER JOIN FETCH applicant.products product ]

解决方案

This should do the trick:

select count(*) FROM Company c JOIN ...

There is no sub-select involved, just instead of returning Company, you return the count.

Edit: The FETCH is out of place now. You're not returning the entities from the query, but only the count, thus, Hibernate complains. Removing it should help:

select count(product) from org.myCompany.applicant.entity.Applicant applicant 
    LEFT OUTER JOIN applicant.products product

这篇关于Hibernate HQL:获取结果的数量而不实际返回它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆