Discussion:
[Hibernate] Criteria.count()
Chris Nelson
2003-11-16 17:01:26 UTC
Permalink
All,

After needing and not finding the Criteria.count()
method and then finding a request for it in JIRA, I
went ahead and implemented it. Though there seems to
be some dissension about this method, not having it
meant I couldn't use the Criteria API in my
application, and I suspect other folks are in the same
boat.

I would like to submit it back to Hibernate. It looks
like submitting a patch thru JIRA is the way to do it.
I built my patch against what is currently checked
into the v21branch and submitted it to JIRA as HB-474.
I hope using the v21branch like this is the best
practice. The downside is that there is a high
potential for conflicts, since Hibernate is such a
dynamic project.

Also, I noticed that several unit tests did not pass
for me using the CVS code. I ran the tests before and
after my changes and the numbers of failing test cases
were the same.

Here's a brief outline of the classes I had to touch
to implement this change:

AbstractEntityLoader - needs to save the select
statement it builds, so it now has a protected Select
member

CriteriaLoader adds a count() method that modifies
this Select statement and runs it. Also refactored
the list method to avoid duplicate code.

Criteria adds the count method.

CriteriaImpl adds this count method, which delegates
to SessionImpl.count which delegates to
CriteriaLoader.count. All this is modeled after the
list method.

Well, I hope this change can make it in. If someone
has a better way to implement this, I'm willing to
refactor this and resubmit it, time permitting of
course. I'm also willing to help out to try to fit
this code into whatever aggregate functionality
develops if that makes sense. I think having that
Select on AbstractEntityLoader may help implement
aggregation, but that is a hopeful guess on my part.
I have tested this with junit tests for my project. I
can also create one in the Hibernate test area, but
haven't done so yet.

Thanks,

Chris Nelson








__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
rjf
2003-11-16 17:47:37 UTC
Permalink
Post by Chris Nelson
After needing and not finding the Criteria.count()
method and then finding a request for it in JIRA, I
went ahead and implemented it. Though there seems to
Chris, funny you should write this. This morning I was lamenting the
fact that I my app, which mostly uses the Criteria API, is becoming
sprinkled with HQL as well, because of the inability to do a few things
via Criteria.

The two things I am missing in the criteria API are the count, as you
mention, and a more generic order by mechanism. I'd like to be able
using the Criteria api to do something like:

crit = session.createCriteria( clazz );
crit.add( Expression.eq( name, "foo" ) );
crit.addOrder( Order.orderBy( "rand()" ) ); <---- ????
crit.setMaxResults(1);

Perhaps this is already possible and I just haven't found how.

And the count functionality would be nice as:

crit = session.createCriteria( clazz );
crit.add( Expression.eq( name, "foo" ) );
int count = crit.count();

I understand the Criteria API is fairly new? I am wondering to what
extent enhancements will be done in this area, and how far Gavin sees it
going.

So, I guess I am just adding another voice of support for enhancements
like this, and in fact was pretty close to tackling the count task
myself. I am happy that you took it on.

Thanks,
rjf&

Loading...