You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by 周雁鸣 <zh...@gmail.com> on 2009/11/23 03:12:19 UTC

How to count on a query then reuse this query fetch data?

CriteriaQuery<User> cq = ...
TypedQuery<User> tq = em.createQuery(cq);

long count = count(tq); //howto?

tq.setFirstResult(0);
tq.setMaxResults(10);
List<User> users = tq.getResultList();

how to implement count function and I can reuse query object to find
entities.

RE: How to count on a query then reuse this query fetch data?

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  You brave hearts seem to have ventured into Criteria Query. How is it
going?

  On this specific question, how about the following

  CriteriaQuery cq = ... // notice the generic type as Object
  
  cq.select(builder.count(whatever))
  Integer tq = (Integer) em.createQuery(cq).getSingleResult();
  // now change the select list
  cq.select(whatUreallyWant);
  List result = em.createQuery(cq).getResult();

 The downside is you need to cast the results.

 Regards --

Pinaki


Michael Vorburger-4 wrote:
> 
> Hi 周雁鸣,
> 
> CriteriaQuery<User> cq = ...
> TypedQuery<User> tq = em.createQuery(cq);
> 
> tq.setFirstResult(0);
> tq.setMaxResults(10);
> List<User> users = tq.getResultList();
> 
> long count = users.size();
> 
> This will, normally, generate TWO SQL queries - a SELECT {columns} ... and
> a SELECT COUNT(id).
> 
> Ok?
>  
> 
> -----Original Message-----
> From: 周雁鸣 [mailto:zhouyanming@gmail.com] 
> Sent: Monday, November 23, 2009 3:12 AM
> To: users@openjpa.apache.org
> Subject: How to count on a query then reuse this query fetch data?
> 
> CriteriaQuery<User> cq = ...
> TypedQuery<User> tq = em.createQuery(cq);
> 
> long count = count(tq); //howto?
> 
> tq.setFirstResult(0);
> tq.setMaxResults(10);
> List<User> users = tq.getResultList();
> 
> how to implement count function and I can reuse query object to find
> entities.
> 
> ____________________________________________________________
> 
> � This email and any files transmitted with it are CONFIDENTIAL and
> intended
>   solely for the use of the individual or entity to which they are
> addressed.
> � Any unauthorized copying, disclosure, or distribution of the material
> within
>   this email is strictly forbidden.
> � Any views or opinions presented within this e-mail are solely those of
> the
>   author and do not necessarily represent those of Odyssey Financial
> Technologies SA unless otherwise specifically stated.
> � An electronic message is not binding on its sender. Any message
> referring to
>   a binding engagement must be confirmed in writing and duly signed.
> � If you have received this email in error, please notify the sender
> immediately
>   and delete the original.
> 
> 


-----
Pinaki 
-- 
View this message in context: http://n2.nabble.com/How-to-count-on-a-query-then-reuse-this-query-fetch-data-tp4048648p4067146.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: How to count on a query then reuse this query fetch data?

Posted by Michael Vorburger <mv...@odyssey-group.com>.
Hi ������,

CriteriaQuery<User> cq = ...
TypedQuery<User> tq = em.createQuery(cq);

tq.setFirstResult(0);
tq.setMaxResults(10);
List<User> users = tq.getResultList();

long count = users.size();

This will, normally, generate TWO SQL queries - a SELECT {columns} ... and a SELECT COUNT(id).

Ok?
 

-----Original Message-----
From: ������ [mailto:zhouyanming@gmail.com] 
Sent: Monday, November 23, 2009 3:12 AM
To: users@openjpa.apache.org
Subject: How to count on a query then reuse this query fetch data?

CriteriaQuery<User> cq = ...
TypedQuery<User> tq = em.createQuery(cq);

long count = count(tq); //howto?

tq.setFirstResult(0);
tq.setMaxResults(10);
List<User> users = tq.getResultList();

how to implement count function and I can reuse query object to find entities.

____________________________________________________________

� This email and any files transmitted with it are CONFIDENTIAL and intended
  solely for the use of the individual or entity to which they are addressed.
� Any unauthorized copying, disclosure, or distribution of the material within
  this email is strictly forbidden.
� Any views or opinions presented within this e-mail are solely those of the
  author and do not necessarily represent those of Odyssey Financial
Technologies SA unless otherwise specifically stated.
� An electronic message is not binding on its sender. Any message referring to
  a binding engagement must be confirmed in writing and duly signed.
� If you have received this email in error, please notify the sender immediately
  and delete the original.