Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate HQL: Get count of results without actually returning them
    primarykey
    data
    text
    <p>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:</p> <pre><code>select Company company LEFT OUTER JOIN FETCH products product </code></pre> <p>I read in the Hibernate documentation that:</p> <blockquote> <p>You can count the number of query results without returning them: </p> </blockquote> <pre><code>( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue() </code></pre> <p>I suspected that I should be replacing the <strong>....</strong> with my query, but that does not work, as HQL does not support sub-selects in FROM.</p> <p>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.</p> <p>Cheers!</p> <p>**UPDATE: ** </p> <p>I used this regex to convert my query:</p> <pre><code>Number num = (Number) em.createQuery(dynamicQuery.replaceAll("select \\w+ from ", "select count(*) from ")).getSingleResult(); </code></pre> <p>And I get this:</p> <blockquote> <p>Blockquote</p> <p>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}}] [<strong>select count(<em>) from org.myCompany.applicant.entity.Applicant applicant LEFT OUTER JOIN FETCH applicant.products product</strong> ]; 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(</em>) from org.myCompany.applicant.entity.Applicant applicant LEFT OUTER JOIN FETCH applicant.products product ]</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload