pagination example in hibernate

In this article we will pagination example with hibernate . In application that uses large data it is always good practice to use pagination. Like in google search we see only first 10 search record on each page like this

google search pagination example

This offer benefit that the search time is less as getting ten record take less time than getting all the records from DB. It also limit page size. So response is quick.

One more benefit is that user only see best record of choice . User dont get confuse with too many record and can see choice record easily among limited record than from all the possible records.

In this article we will see example of product list. There are many product in DB . We will show only ten products at a time. It should look like this hibernate pagination example

The directory structure  is shown below for this example . Code for this article is provided on javafreakers github director.  There are three package of java classes . Controller package contain class which take request and return response. dao packge contain interface ProductDao  and its implementation package daoImpl which contain class ProductDaoImpl . This class contain DB transactions code. model package contain class which is simple pojo class.

Web-Inf contain  views folder and classes folder. In views folder there is a jsp file which disply products detail. classes folder contain spring-config package which contain file applicationContext-hibernate.xml for hibernate configurations information. file contain database regarding information i.e DB name, Driverclass name etc.

spring-servlet.xml file contains spring beans information.

hibernate paginator example directory structure

Now let see coding part. Although most of code is explained with comments but still i will try to explain it in detail.


web.xml file contain root configuration information. <servlet-mapping> element tell that any request with .html extension will be delivered to spring and handled by spring.

contextConfigLocation will help to locate configuration file from class path.


spring-servlet.xml define spring  specific beans. The Element <context:component-scan> Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-
provided @Component, @Repository, @Service, and @Controller stereotypes will be detected.  InternalResourceViewResolver bean is used to return response with the help of  prefix and suffix property. Spring will scan  prefix folder for suffix file extension to return response to user. In this example InternalResource ViewResolver will look in directory /WEB-INF/views/ for .jsp file. The name of file is return from controller class.

ProductDaoImpl is laso registred as spring bean.


These are xml configuration for hibernate. We have used PropertyPlaceholderConfigurer bean to get database configuration information from file. It use annotatedClasses properties to locate Entity classes which are configured with @Entity JPA annotation. In this example class is configured with this annotation.

jdbc.prperties file contain databse name , database driver name, database username and password.


Redirect to another page which is ProductHome for product list.


On this jsp page we are displaying records that we have set in session in controller class. This code is explained fully in comments.

Controller class listen user request and return response. In this we have a method inserProduct() which is basically to insert record in DB.

productsHome method is all logical part that is managing pagination functioning. Most of things have been cleared in comments of the code. When user request first time then we are returning first ten record . After that when user request with specified page number which is offset we are returning first ten record starting with offset number*10.

We have set size of total number of record , page number list that is to be display on jsp page and  product list in session.

This is interface for ProductDaoImpl class. It has three method. Detail of these method given below in  ProductDaoImpl. have three methods . One saving records in DB. One is for getting total numbers of record in DB. One method for getting product list for particular page. In the method getProduct() we have used criteria API for  getting Record for a particular page number. We use criteria.setFirstResult( ) method to tell hibernate to tell the starting record number . I.e first record and criteria.setMaxResults() to tell the number of record that need to be fetch after first record. For example suppose we have set first record as 15 and max record as 20 then hibernate will get us record from 15 to 34 .

In getSize() method we have again used Criteria to know total number of record present in Product table. is simple pojo class with properties and its getters and setters method. There is nothing to explain in it.

When you run this project first time then replace productsHome.html in index.jsp file with addProduct.html so that it can insert some record in DB. After that Undo code of index.jsp so that when you run this project again it display product table. Below is onload screen. Here start is not visible because we are in beginning.

hibernate paginator example onload first page screenshot

Here is screen when we click any page in between start and End . User can click on start ot End to go in beginning or End directly.

hibernate pagination example

When User is at the end page then End button is not visible but start button is visble to take user in beginning directly.

hibernate paginator example last page screenshot


Well that is all in this post of hibernate tutorials. Hopes this pagination article will help you  a lot. I will come back soon with next article. Till then take care stay happy stay blessed and enjoy.

Comments are closed.