-->
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.  [ 4 posts ] 
Author Message
 Post subject: Pagination Example
PostPosted: Tue Jun 15, 2004 4:15 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Hi,

I have taken the time to read all the posts here on Pagination and trying to implement pagination. However, I have not seen one example of anyone coming out and saying "Hey ! I've done it in Hibernate and here is some pseudocode". I have read about setMaxResults() and setFirstPage(), but I am still not sure of how to get a count of the total results in the resultset without issuing a .list().size(). But to get a total count of results, I would have to do a .list().size() on the Query *without* the setMax() and setFirst() and then execute the Query *with* the setMax() and setFirst() to later just limit the results to 5 or 10 or what have you per page.

So in short, is there anyone that has implement pagination in hibernate successfully *using Query* ? If so, can someone please share some ideas or some code with me to help me out ?

Thanks in advance !
S


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 15, 2004 4:24 pm 
Newbie

Joined: Fri Apr 23, 2004 4:18 pm
Posts: 15
Location: Oslo, Norway
See http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html#queryhql-tipstricks.

Simply do a select count(*) something in session.iterate() or session.find().
Code:
( (Integer) session.iterate("select count(*) from ....").next() ).intValue()
.




Top
 Profile  
 
 Post subject: But what about params ?
PostPosted: Fri Jun 18, 2004 9:26 am 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Hi,

Thanks for the reply. But how does this work for parameterized queries ? I have a query where I need to specify parameters, but at the same time, I need a count of how many records it will return. Your examples was simpler - just a select with no params. I have params in my query. Can someone pls point me in the right direction ?

Thanks !
S


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 10:29 am 
Newbie

Joined: Fri Apr 23, 2004 4:18 pm
Posts: 15
Location: Oslo, Norway
Simply do

Code:
Integer count = (Integer) session.createQuery("select count(*) from com.Foo  as foo where foo.bar = :bar")
    .setParameter("bar", "bas")
    .uniqueResult();





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