I have an issue where the code at the layer I am working in receives a CriteriaQuery object. I have virtually no other information about the query (though I can make a good guess as to the type of object or object list it should return.) What I need to do is determine the number of items it will return without creating all the objects.
What I would like to do is something like SELECT count(*) from ([SQL version of the CriteraQuery])
I have tried to create a new CriteriaQuery and use the CriteriaQuery parameter as a subquery but could not find a way to do that.
I tried to parse apart the CriteriaQuery to construct a proper count() query, but no luck determining the various parts of the where clause.
I could not find a way to retrieve the SQL statement from the TypedQuery that is generated from the CriteriaQuery.
So, short of something like the following:
long getCount(CriteriaQuery<?> query)
{
return entityManager.createQuery(query).getResultList().size();
}
is there anyway to determine the number of objects/rows that will be returned by a CriteriaQuery?
This is different from In JPA 2, using a CriteriaQuery, how to count results in that that question deals with a case where one is building a CriteriaQuery. In this case we are given a constructed CriteriaQuery (e.g. public int getCount( CriteriaQuery query ))