-->
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: Projection row count
PostPosted: Thu Mar 09, 2006 11:25 am 
Newbie

Joined: Fri Aug 06, 2004 1:01 pm
Posts: 13
Hi,

I am using some pagination to display my objects.
So I need to get my list ob objet and the total number of objects ...

In version 2, I used Criteria.count() to get the total number of results.


I just moved to version 3. So now I will have to use projections.
I just added it in my code with :

criteria.setProjection(Projections.rowCount());

The thing is, it modifies my criteria. SO, then, want I want to retrive my list of result I can't do it anymore !!!

Should I duplicate my criteria ? Then if yes, how, since I don't see any method for it...

My previous code :

Code:
   public Page(Criteria criteria , String noPage, int pageSize)
         throws HibernateException {
      this.page = new Integer(noPage).intValue();
      this.pageSize = pageSize;
      int initialRow = page * pageSize;
      int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant
      
      criteria.setFirstResult( initialRow );
      criteria.setMaxResults(maxNbRows);
   
      // calcul le nombre de total de resultats
      this.totalResults = new Long(criteria.count()).intValue();
            
      results = criteria.list();
   }


My actual code that does not work :

Code:
   public Page(Criteria criteria , String noPage, int pageSize)
         throws HibernateException {
      this.page = new Integer(noPage).intValue();
      this.pageSize = pageSize;
      int initialRow = page * pageSize;
      int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant
      
      criteria.setFirstResult( initialRow );
      criteria.setMaxResults(maxNbRows);
   
      Criteria countCriteria = criteria.setProjection(Projections.rowCount());
      Integer result = (Integer) countCriteria.uniqueResult();
      this.totalResults = result.intValue();
            
      results = criteria.list();
   }


Note : I was thinking to just retrieve the row count after retrieving my list. That works, but only when I don't use setFirstResult and setMaxResults since it seems row counting does not like to have "limit ?,?" in the query. ANd I need that for my pagination !!!

thanks for help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 12:01 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
I had the same problem implementing a kind of a db search...

I think there is no other way than retriving the count after the result.
I modified you code a bit to get the rowCount working...

Code:
public Page(Criteria criteria , String noPage, int pageSize)
         throws HibernateException {
      this.page = new Integer(noPage).intValue();
      this.pageSize = pageSize;
      int initialRow = page * pageSize;
      int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant
     
      criteria.setFirstResult( initialRow );
      criteria.setMaxResults(maxNbRows);
     [b] results = criteria.list();
      criteria.setFirstResult(0);
      criteria.setProjection(Projections.rowCount());
      Integer result = (Integer) countCriteria.uniqueResult();
      this.totalResults = result.intValue();[/b]
           

   }


simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 12:09 pm 
Newbie

Joined: Fri Aug 06, 2004 1:01 pm
Posts: 13
good workaround... :-)
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 10:43 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
Quote:
I think there is no other way than retriving the count after the result.


I have posted a similar question on the oracle forum and received the following responses: http://forums.oracle.com/forums/thread.jspa?forumID=75&threadID=510229

So it does appear that there is a way to do this without having to execute a sql statement twice and thus incur twice the performance cost.

Does anyone know how to create a criteria object that will allow Oracle (or any database for that matter) to return the count and the page in the same result set as the oracle forum suggests?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.