Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Get result count from criteria query
Get result count from criteria query [message #724377] Mon, 12 September 2011 08:37 Go to next message
dreske Missing name is currently offline dreske Missing name
Messages: 17
Registered: August 2010
Junior Member
Hello,

I build an criteria query, to get some entities from the database.
For pagination reasons, I set firstResult and maxResults to get only a subset of the total result.

Is there a way to get the whole entity count of this query, without manually building a second count query?
This is because I want to build a method in my service base class, that returns the paginated result, by giving it only the query object that returns the results.

I imagine something like this:

public<T> PaginatedResult<T> executeQuery(CriteriaQuery<T> query, Paginator paginator) {
 ....
}


The Paginator object includes firstResult and maxResults and the PaginatedResult contains the List<T> and a totalResults fields.
So the concrete service implementation has to build the query, to get its data, and the above method executes this and gets the data AND the total results, this query would return without pagination.
My problem is, that I have to build a second "count query", but in this method I've no informations about the where-clauses, etc attached to the query.

thanks
Dirk
Re: Get result count from criteria query [message #724593 is a reply to message #724377] Mon, 12 September 2011 15:57 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

There is no standard method in JPA for this.

You could pass an inner class instead of the CriteriaQuery that knows how to build the query, then add a count to it.

With EclipseLink you could create a Query from the CriteriaQuery, cast it to a JpaQuery and get the DatabaseQuery from it. You could then build a ReportQuery with the DatabaseQuerys selectionCriteria and add a count.


James : Wiki : Book : Blog : Twitter
(no subject) [message #724603 is a reply to message #724377] Mon, 12 September 2011 15:57 Go to previous message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
There is no standard method in JPA for this.

You could pass an inner class instead of the CriteriaQuery that knows how to build the query, then add a count to it.

With EclipseLink you could create a Query from the CriteriaQuery, cast it to a JpaQuery and get the DatabaseQuery from it. You could then build a ReportQuery with the DatabaseQuerys selectionCriteria and add a count.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Previous Topic:(no subject)
Next Topic:(no subject)
Goto Forum:
  


Current Time: Fri Oct 24 14:49:55 GMT 2014

Powered by FUDForum. Page generated in 0.01756 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software