Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate HQL: Get count of results without actually returning them

Tags:

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 ]

like image 704
Markos Fragkakis Avatar asked Feb 04 '10 09:02

Markos Fragkakis


People also ask

How can I get HQL query results?

You have to do the following: final String hql = "select app. appkey,app. type from " + getClassName() + " app where app.

What is the description for count () in hibernate?

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.

IS NULL check in HQL?

No. You have to use is null and is not null in HQL.

What is difference between HQL and Criteria in hibernate?

HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries. HQL is to perform both select and non-select operations on the data, Criteria is only for selecting the data, we cannot perform non-select operations using criteria.


2 Answers

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
like image 143
Henning Avatar answered Oct 13 '22 05:10

Henning


you can get the count of the query result by using:

criteria.setProjection(Projections.rowCount());
count=(Long) criteria.uniqueResult();

hope this helps

like image 45
Alok Bhandari Avatar answered Oct 13 '22 05:10

Alok Bhandari