-->
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.  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Sun Jun 19, 2005 8:20 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
Paule,

I agree that count() functionality is universal and required in pretty much all non-trivial applications, and that therefore it was very surprising to me that the early Hibernate API provided support only reluctantly in patch form!

In the real world, every hibernate developer was working with patched jars and/or home grown "double criteria" hacks to accomplish one of the most common use cases!

An API should be designed for its commonest usage scenarios. All applications building interactive queries (i.e. probably any decent size webapp using hibernate) surely need pagination functionality. You rarely want to say "give me an unlimited number of results and send them all back to the client".

Luckily H3 provides the projections technique to accomplish this without patching. BTW the documentation didnt help me figure that out, I had to look at the source code.

As for using perl, well that's not such a bad idea ;) We've been looking at how we can speed up our development process. Writing Java web apps using standard "best practice" is incredibly inefficient. In the time it takes to build a simple CRUD app in Java you can write a whole portal in PHP or mason. However, tools like Eclipse and Hibernate are awesome and make us stick with Java, but just adapt our development models so we can work faster and smarter.

There's a reason Amazon use perl, Yahoo use PHP etc...but we're sticking with JSTL/freemarker for now.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 19, 2005 8:24 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Provide a documentation patch if you don't like the docs.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:28 am 
Newbie

Joined: Mon Jun 12, 2006 10:32 am
Posts: 3
once you set projection to null you cant re-use projection with same criteria even with a detached criteria...any work around?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:15 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
This thread is getting near to my post in this forum:

http://forum.hibernate.org/viewtopic.php?t=956564&highlight=pagination

I am looking for a way to use hibernate to get not only the pagination, but also the row count in a single statement. Taking the approaches in this thread would require you to do TWO distinct sql queries.

Using straight Oracle SQL there appears to be a way, but I don't know how to construct the Criteria object.

http://forums.oracle.com/forums/thread.jspa?threadID=510229

Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 1:14 pm 
Newbie

Joined: Fri Oct 31, 2008 12:21 pm
Posts: 4
Dencel wrote:
beniji wrote:
Guys

You dont need to do the "double criteria" hacking.

After you get the row count, just do this (on the same Criteria):

Code:
crit.setResultTransformer( Criteria.ROOT_ENTITY );
crit.setProjection( null );


.. and hey presto you can re-use the same criteria object.

HTH,
Ben


Actually, it's the other way arround:
Code:
crit.setProjection( null );
crit.setResultTransformer( Criteria.ROOT_ENTITY );


setProjection sets the ResultTransformer to PROJECTION.


This approach doesn't work if the original criteria contained a projection, since you are overwriting it with null. I think you need to save the original projection that the criteria might contain AND the original ResultTransformer and restore them afterwards (as opposed to just setting it to null).

I describe my approach in the following thread: http://forum.hibernate.org/viewtopic.php?t=974802 (Pagination with Projection.rowCount() in a single call?).

(The fourth post from Octuber 31)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 12:53 pm 
Newbie

Joined: Fri Apr 13, 2007 12:25 pm
Posts: 17
I am also implementing pagination with count by manipulating the projection for two different queries (page, count). I have set up a function to abstract pagination for me, to which I pass a Criteria object. The problem I am seeing is that the count query is significantly slowed down by the "order by" clause, which is unnecessary. I am getting queries that end up looking like this:

select count(this_.id) from employee order by hireDate desc

The order by clause in this case is unnecessary. I would like to run the count query second so that I can wipe out the order by, but it looks like all you can do is append to the CriteriaImpl's List of order by clauses.

Do you guys know any way around this other than modifying the CriteriaImpl class to have a removeOrder function?


Top
 Profile  
 
 Post subject: Re: Be nice if they embraced the criteria.count() method
PostPosted: Wed Mar 06, 2013 3:06 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
paule wrote:
Now I'm trying to upgrade to Hibernate 3. And the code is looking worse. And type safety is reduced.
So now from what I get from the new docs:
long total = criteria.list();
becomes:
Integer count = (Integer)c.setProjection(Projections.rowCount()).uniqueResult();
int total = count.intValue();
Besides more code to do the same thing, we have to cast it to an Integer (lack of type safety) and the criteria object gets modified in the process..


Hi Paule,

I always wondered why is uniqueResult returning a Long and setFirstResult requiring an int ?
Code:
totalSize = ((Long) countCriteria.uniqueResult()).intValue();


Kind Regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page Previous  1, 2

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.