0

In pagination I am having 1000 records but by default it should load only 100 records first and remaining should not load until I fetch those records from database.

Please help me.

Thanks in advance for your help.

Habib
  • 846
  • 2
  • 9
  • 24
sana
  • 3
  • 5

1 Answers1

0

Its very simple. In each pagination setFirstResult will be changed. Using:

query.setFirstResult(0);
query.setMaxResults(100);

Duplicate of hibernate pagination mechanism

Community
  • 1
  • 1
Arindam
  • 555
  • 1
  • 8
  • 24
  • thank you for reply, even i tried it but its just showing only 100 records. so, how can i fetch remaining records from 101 to 1000 ...this is my problem – sana Mar 23 '17 at 06:32
  • For second page, you need to change FirstResult set to 100. E.g. for second page query.setFirstResult(100); again next page query.setFirstResult(200);. But the MaxResults will be the same 100 as you want to fetch maximum 100 records each time. – Arindam Mar 23 '17 at 06:48
  • i tried it here but no result of using setFirstResult() – sana Mar 23 '17 at 07:14
  • This is not the way to make pagination. For first request, you need to have query.setFirstResult(0); query.setMaxResults(100); For second request you need to have query.setFirstResult(100); query.setMaxResults(100); For third request query.setFirstResult(200); query.setMaxResults(100); and so on... – Arindam Mar 23 '17 at 10:51
  • how we will write first request, second request and so on .. can u give an example? – sana Mar 23 '17 at 12:01
  • Example like: in UI side you have JSP and in Backend you have Servlet and for DAO layer you have Hibernate. Now suppose you are fetching employee records by pagination. In down of the employee table grid, previous and next link is there. For first page FirstResult as 0 sent from UI. While you click Next link you need to send page value is 1 and in hibernate side you need to set FirstResult as (1+100), here 1 is the page value and 100 is the MaxResults and make it (1+100)-1, here means FirstResult you set as 100. So FirstResult should be dynamic as input and here I am getting the input from UI. – Arindam Mar 23 '17 at 13:28