-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to fetch total record count for any given criteria
PostPosted: Thu Aug 23, 2007 10:26 pm 
Newbie

Joined: Mon Nov 20, 2006 4:08 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 5.0

The generated SQL (show_sql=true):


Hey.
Wish every goes well with your hibernate life.

I got an issue the i need to write i paging api.
so i need to know the total record for a give query

I have implemented HQL related function
but how to get it work with criteria?

Any suggestions?

Like how to convert
Code:
select count(*) from (select * from tbl_user a) b;


criteria query where select * from tbl_user is original criteria


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 1:18 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

session.createCriteria(tbl_user.class)
.setProjection( Projections.projectionList()
.add( Projections.rowCount() )
)
.list();



Amila

(Don't forget to rate of helps)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 25, 2007 11:39 pm 
Newbie

Joined: Mon Nov 20, 2006 4:08 am
Posts: 3
well, it's not that simpile

I mean with a GIVEN criteria

here is my current code, i wonder if there would be a better solution.


Code:
   private int getRecordCount(DetachedCriteria criteria) {
      Criteria c = criteria.getExecutableCriteria(getSession());
      CriteriaImpl ci = (CriteriaImpl) c;
      int count = 0;
      try {
         Object orderEntries = BeanUtils
               .forceGetProperty(ci, "orderEntries");
         BeanUtils.forceSetProperty(ci, "orderEntries", new ArrayList());

         Projection proj = ci.getProjection();
         if (proj == null) {
            Object obj = ci.setProjection(Projections.rowCount())
                  .uniqueResult();
            count = ((Number) obj).intValue();
         } else {
            count = ci.list().size();
         }

         BeanUtils.forceSetProperty(ci, "orderEntries", orderEntries);
         ci.setProjection(proj);

      } catch (NoSuchFieldException e) {
         throw new InternalError(e.getMessage());
      }

      return count;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 1:07 pm 
Newbie

Joined: Fri Oct 31, 2008 12:21 pm
Posts: 4
I'm using a similar approach as described in the previous post, but found out that you also need to save the original ResultTransformer to restore the state of the original Criteria.

I describe my approach in the following thread: http://forum.hibernate.org/viewtopic.php?t=974802 (Pagination with Projection.rowCount() in a single call?).

(See the fourth post, from Octuber 31)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.