-->
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.  [ 3 posts ] 
Author Message
 Post subject: do select(*) with max number of results w/ Criteria api
PostPosted: Tue Aug 26, 2008 1:04 pm 
Newbie

Joined: Tue Aug 26, 2008 12:52 pm
Posts: 2
Hi,

For performance reasons, I want to limit the number of counted rows in a count(*) query. I can do this in sql as follows:

select count(*) from
(select
id
from
USER u
where
...
limit 10000
) as tmpResult

I've been trying to do this using the Hibernate Criteria API but unfortunately I'm getting nowhere.

Is such a simple thing possible?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 12:02 pm 
Newbie

Joined: Wed Mar 05, 2008 11:26 am
Posts: 8
I think you simply want to look at .setMaxResults(int maxResults).

I use a rather complex query, and the criteria will change dynamically based on the args I pass into my method; I reserve the sort order and max results for the tail end.

AJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 12:18 pm 
Newbie

Joined: Tue Aug 26, 2008 12:52 pm
Posts: 2
ajile81 wrote:
I think you simply want to look at .setMaxResults(int maxResults).

I use a rather complex query, and the criteria will change dynamically based on the args I pass into my method; I reserve the sort order and max results for the tail end.

AJ


Thanks, but that doesn't solve the problem: adding a limit to a plain-jane count(*) query will not work as the query only returns 1 result - the result of the count. It will not stop the database from counting up to the limit, hence my subselect above.

I have a sort of dirty workaround with a custom postgres (my database) dialect:

Code:
public class CustomPostgreSQLDialect extends PostgreSQLDialect {
   
   
   @Override
   public String getLimitString(String sql, boolean hasOffset) {
      // check if this is a count query
      if (sql != null && sql.toLowerCase().startsWith("select count(*) as y0_")) {
         // replace count(*) with *
         String newSql = sql.substring("select count(*) as y0_".length());
         newSql = "select *" + newSql;
         // and rewrite as a subquery
         newSql = "select count(*) as y0_ from (" + newSql + " limit ?" ;
         if (hasOffset) newSql += " offset ?";
         newSql += ") as ___tmpResult";
                  
         return newSql;
      } else {
         // regular query, delegate to superclass
         return super.getLimitString(sql, hasOffset);
      }
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.