1

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 ))

Community
  • 1
  • 1
cWarren
  • 453
  • 5
  • 11
  • 2
    have seen this? http://stackoverflow.com/questions/2883887/in-jpa-2-using-a-criteriaquery-how-to-count-results – bpgergo Aug 15 '11 at 23:34
  • @bpgergo I had not seen that, but it does not answer my question. only have a CriteriaQuery object and can not figure out how to create another one that does a count(). – cWarren Aug 15 '11 at 23:50
  • The question @bpgergo pointed to has multiple answers. The accepted answer however is not the one you need, however reyiyo posted an answer that can get you a long way. Why the OP in the referenced question didn't accepted that answer isn't clear to me. – siebz0r Jun 05 '12 at 09:15

0 Answers0