• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Hibernate alone implement pagination?

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to implement a pagination concept, where a page shows only a limited subset (say, 10 Items) at a time, and users can navigate to the next and previous pages manually.



This code produces 1st 20 records. But what about navigation to another page, where next set of 20 records are displayed?

Can Hibernate alone solve this problem, without going into other libraries like Display tag libraries & Jsp taglib?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kunal,

you can do that by using setFirstResult() method.

Check this Example
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hemant Thard. It fetches me required number of records in a jsp page. But how to navigate to another page where it will fetch me another set of records. Should i run the servlet & Dao class again?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to do is a little math. No frameworks needed for that.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault.
I have fetched all the 30records, but i need to display them 5 at a page, each record with 2 radio buttons.

Should i call servlet & dao class again,but that will be a lot of db hits? Or should i have a link in jsp page invoking the dao code with next page number appended to the url?

Or should i invoke the dao code from jsp with page number incremented by 1? No i guess that's a bad idea

Can you please provide me some hint?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One option is to use code something like this



Or, put the code which are in scriplet into a servlet, and call it each time . But, will that be efficient, if need to go for 70Records, 5 at a time, that too with 2 radio buttons, for getting user choice?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing setFirstResult() returns the record stating from the arg value. But i need random records
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since i have the list of random Records, i should go for some that implements it in jsp itself, any way of iterating into the list for 5records
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault, what should i do?
 
Ankit Kumar Yadav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kunal, if you got the solution, let us know. i am facing the same problem
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is solved by implementing pagination in jsp, not via hibernate
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to do is keep track of which page number you want to look at, and do a little math to figure out what the value to pass to setFirstResult().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic