Hibernate HQL: Get count of results without actually returning them

后端 未结 2 1564
遇见更好的自我
遇见更好的自我 2020-12-15 20:02

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:

2条回答
  •  醉梦人生
    2020-12-15 20:57

    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
    

提交回复
热议问题