Recent Posts

Saturday, 3 September 2016

Hibernate Pagination Tutorial

Pagination using Hibernate
     In general when we are doing search, the search may return huge number of results. But at a time if we try to display those results in the web page, it leads to the following problem
1. Takes more time to load
2. Takes more memory to hold all results
3. Difficult to navigate over huge number of records

To solve this problem we can go for pagination.

What is Pagination?
     Pagination is the process of dividing content into multiple pages. 
     In google search when we search, we will get the huge number of search results. But they will display only 10 links per page. And below the page they will display page numbers to navigate to other pages. This is nothing but pagination.

     Pagination can be implemented in two ways.
1. Front-End pagination
2. Back-end pagination
1. Front-End pagination
     In Front-End pagination, we will hit the database only once, and get the results and store them in the memory (session/application scope). When user navigating from one page to another, we retrieve data from memory but not hitting the database again and again.
     With this style of pagination we can achieve easy navigation, fast loading of the data. But still we have the problems are there like
1. Takes more memory to hold all results
2. If data is updated in the database by other application, we can't get updated data, As we are taking the data from memory instead from database.

2. Back-End Pagination
     In Back-End Pagination, we will hit the database again and again, and get the requested page details from the database.
     With this style of pagination we can achieve easy navigation, fast loading of the data, takes memory to hold the result data and it will get always updated data. But it gives lessa performance when compared with front-end pagination.
     This type of pagination is suggestible, if number if resulted records are unlimited and the data is not constant data.
     For back-end pagination, we need to write a query in such a way that, it returns only the requested page details.
     To implement this queries different databases using different queries. Like the database to database the query information is different. To overcome this problem, we can use Hibernate support. To apply pagination hibernate provides two methods in org.hibernate.Query interface.
1. setFirstResult(int firstResult)
2. setMaxResult(int maxResults)
setFirstResult(int firstResult)
     take the argument which specify from where records has to take.
setMaxResult(int maxResults)
     take the argument which specify how many records has to take.
Code Block
String hqlQuery = "From Account a";
Query query = session.createQuery(hqlQuery);
query.setFirstResult(3); // Means start from 4th record
query.setMaxResults(5); // Means per page 5 records to be displayed
List<Account> accounts = query.list();
for(Account account: accounts) {
   System.out.println("Account ID : " +account.getAccountId());
   System.out.println("Name : " +account.getName());
   System.out.println("Balance : " +account.getBalance());
     While using the pagination, In web page we are responsible to display the page numbers, And when the user click on some page number, we need to send the request to server, and get that corresponding page results. To do all these things we need to implement some logic. This logic on the web page if want we can implement on our own or we can use some third party provided custom tags. One of such custom tags is <display> tag. This tag is given by Apache people.

Next Tutorial  HQL Subqueries Tutorial

Previous Tutorial  Hibernate Query Language HQL Part 3 

No comments:

Post a Comment