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.  [ 2 posts ] 
Author Message
 Post subject: count criteria results wirhout initializing it
PostPosted: Mon Sep 19, 2005 3:09 am 
Newbie

Joined: Mon Sep 19, 2005 3:08 am
Posts: 19
Q: How to count results with criteria without initializing whole collection.
I have pageable page.
When I do search, I return only 10 results per page, but I need total number of results to show on page.

Code:

Example ex = Example.create(user).ignoreCase().enableLike(MatchMode.ANYWHERE);
Criteria c = session.createCriteria(User.class).add(ex);
c.setFirstResult(10);
c.setMaxResults(20);
return c.list();

1. Without
c.setFirstResult and c.setMaxresults hibernate works 6 sec, so c.list().size() is very slow,

2. With
session.createQuery("select count(*) from User ").list().get(0);
hibernate works 26 msec
BUT I do not want to use query, I want to count results with criteria. Is that possible?

Idea:
Criteria c = session.createCriteria(User.class, "count(*) as id").add(ex);
~select id, first_name, ...all property from User, count(*) as id..
Is there a option to change selct clause in criteria expression (to remove all expcept count(*))?


Top
 Profile  
 
 Post subject: solution (i found on this forum :-)#
PostPosted: Mon Sep 19, 2005 3:44 am 
Newbie

Joined: Mon Sep 19, 2005 3:08 am
Posts: 19
Example ex = Example.create(user).ignoreCase().enableLike(MatchMode.ANYWHERE);
Criteria c = session.createCriteria(User.class).add(ex);
List c1 = c.setProjection(Projections.rowCount()).list();

It is little slower than
select count(*)

~ 20-30 msec, but it's ok.


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