Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Get result count from criteria query
Get result count from criteria query [message #724377] Mon, 12 September 2011 04:37 Go to next message
Eclipse UserFriend
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 11:57 Go to previous messageGo to next message
Eclipse UserFriend
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.
(no subject) [message #724603 is a reply to message #724377] Mon, 12 September 2011 11:57 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 05:00:23 EDT 2025

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

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

Back to the top