Hibernate HQL: Get count of results without actually returning them

后端 未结 2 1565
遇见更好的自我
遇见更好的自我 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:42

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

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

    hope this helps

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题