-->
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.  [ 8 posts ] 
Author Message
 Post subject: Criteria query & count
PostPosted: Thu Oct 02, 2003 1:49 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I'm using Criteria query build at runtime (from search form fields).
I need to know how many rows query will return. (in other words I need SELECT COUNT(*)). How can I do that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I am not that familar with the new query api but I believe the focus has been on the where clause side rather then the select thus I don't believe you can at this point. You could always use the iterator, eg, ( (Integer) session.iterate("select count(*) from ....").next() ).intValue()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 6:36 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
With iterator I have to construct query by myself. With Criteria I only need to tell Hibernate I'm interested in objects of class A with property X equal to 1 and property Y in given set etc. and Hibernate constructs correct query.

Criteria seems very useful for search when search condition is constructed dynamically from web form. But if I need to manually construct the same query on HQL just to know COUNT(*), there is no reason for me to use Criteria at all :(


Top
 Profile  
 
 Post subject: Criteria.count() on the road map?
PostPosted: Wed Oct 29, 2003 1:27 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
I would very much like a Criteria.count() method for results pagination.
I noticed that there is a request for this functionality in JIRA dating back a few months and I'm just wondering if an implementation is on the horizon?

Thank-you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
One of the things I have learned is "don't implement things before you are ready".

ie. if it looks like someday the Criteria API might support full projection/aggregation, but you don't know what a nice API for this would look like, DO NOT provide a silly countStar() method that you will have to remove later!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 7:07 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Gavin,
.count() method is really needed. Only Hibernate knows how to produce _proper_ counting query for the given database dialect. Without such an API, weird tricks have to be used (see http://forum.hibernate.org/viewtopic.php?t=925140 for example). For me personally, Criteria API is absolutely useless until I can get .count(). I understand what I am only a tiny part of Hibernate community and a lot of people live fine without .count(), but as I see from JIRA, I'm not the only one who needs this!

When someday Criteria API will support aggregation, you can easily deprecate that silly .countStart(). But for now even silly countStart is better than nothing!

Btw, I was thinking .count() would be a good method not only for Criteria, but for any Query...

PS: Even if you won't decide to implement .count(), thank you for you work because Hibernate is a great thing.


Top
 Profile  
 
 Post subject: Just wondering...
PostPosted: Thu Oct 30, 2003 12:44 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Okay, sorry Gavin, I gather you think some (most? ;) ) questions are dumb... but, wouldn't a .count() be a rather "simple" and benign thing? (i.e. build query where clause as usual but instead of putting the fields in put a count(*)? and of course only run such if it is called). Why would you need to rip it out later? (sorry I know nothing about projection or aggregation or how they apply)

I understand now it's not a priority, if even a consideration, which is all I was wondering in the first place.

Thanks.

P.S. If I tried to write my own would that be considered a bad thing?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 5:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Okay, sorry Gavin, I gather you think some (most? ;) ) questions are dumb


Not at all. I just think that some questioners are lazy. People whine continually about how opensource software is undocumented. Well we have a project that is very well documented, but some people refuse to actually read the docs properly and post questions here that are answered in the docs.

Whats more, some people post to the forum the second they see a stack trace. They don't step through code, they don't try to isolate the problem, they don't even try to understand what is going on. These people get my grumpy responses.

Every hour that I spend answering forum posts is an hour I can't spend on developing Hibernate or writing documentation. It is by far the most timeconsuming part if my job here. I'm not sure if people properly appreciate the fact that this forum can take up to three hours a day for me, every day.

Sure, it would be nice if I could be polite and friendly all the time, and I am trying to improve on this front, but really, if people want handholding, they should hire one of us to come onto their site on a consulting basis (this is now possible). I don't mind if you ask lazy questions of someone you are paying for their time.

Now, Christian keeps yelling at me for posting here so much and tells me to answer much fewer questions. I am trying to take this advice. Its better if I ignore a post than give a grumpy response.

END RANT

As for count(), what you need to understand is that the only way Hibernate APIs don't get out of control through 1000's of methods with 100's deprecated is that I am an absolute nazi about not adding new methods to core APIs. You are not the only guy requesting new API methods here!

Getting an API method signature wrong causes an intense amount of pain. Hibernate2 is much less than a year old and I already know of some mistakes in package and class naming. I do not want to make any more in a hurry.

APIs need to grow slowly. This means saying no to people sometimes.


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