Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate JSP

In this section, you will learn about Hibernate with JSP using simple pagination example.


How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

Hibernate JSP

In this section, you will learn about Hibernate with JSP using simple pagination example.

In the below example, we will describe you how to do pagination using simple JSP and Hibernate.

The project hierarchy and the jar files used is given below :

The SQL query used to create table contact is given below:

CREATE TABLE `contacts` ( 
`id` int(11) NOT NULL auto_increment, 
`firstname` varchar(30) default NULL, 
`lastname` varchar(30) default NULL, 
`cell_no` varchar(15) default NULL, 
`email_id` varchar(30) default NULL, 
`website` varchar(150) default NULL, 
`birthdate` date default NULL, 
`created` timestamp NOT NULL default CURRENT_TIMESTAMP, 



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="devmanuals.model.Contact" />


package roseindia.model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

public class Contact implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long id;
private String firstName;
private String lastName;
private String emailId;
private String cellNo;
private Date birthDate;
private String website;
private Date created;

public Long getId() {
return id;
public String getFirstName() {
return firstName;
public String getLastName() {
return lastName;
public String getEmailId() {
return emailId;
public String getCellNo() {
return cellNo;
public Date getBirthDate() {
return birthDate;
public String getWebsite() {
return website;
public Date getCreated() {
return created;
public void setId(Long id) { = id;
public void setFirstName(String firstName) {
this.firstName = firstName;
public void setLastName(String lastName) {
this.lastName = lastName;
public void setEmailId(String emailId) {
this.emailId = emailId;
public void setCellNo(String cellNo) {
this.cellNo = cellNo;
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
public void setCreated(Date created) {
this.created = created;
public void setWebsite(String website) { = website;

package roseindia.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class DAO {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;
private static int pageSize = 3;
public static List getData(int pageNumber) {
try {
Configuration configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
Session session = sf.openSession();
List result = null;
try {
Query query = session.createQuery("from Contact");
query = query.setFirstResult(pageSize * (pageNumber - 1));
result = query.list();
} catch (Exception e) {
return result;


<%@ page contentType="text/html; charset=UTF-8"%>
<[email protected] uri="" prefix="c"%>
< contentType="text/html; charset=UTF-8" />

<link rel="stylesheet" type="text/css" href="css/screen.css" />
int pageNumber=1;
if(request.getParameter("page") != null) {
session.setAttribute("page", request.getParameter("page"));
pageNumber = Integer.parseInt(request.getParameter("page"));
} else {
session.setAttribute("page", "1");
String nextPage = (pageNumber +1) + "";
session.setAttribute( "EmpList", roseindia.dao.DAO.getData(pageNumber));
String myUrl = "contact.jsp?page=" + nextPage;
pageContext.setAttribute("myUrl", myUrl);
<h2>Showing Table Records</h2>
<jsp:useBean id="EmpList" scope="session" type="java.util.List"></jsp:useBean>
<table border="1">
<th>Web Site</th>
<th>Creation Date</th>

<c:forEach items="${EmpList}" var="emp" begin="0" end="10">
<td><c:out value="${}"></c:out>&nbsp;&nbsp;&nbsp;</td>
<td><c:out value="${emp.firstName}"></c:out></td>
<td><c:out value="${emp.lastName}"></c:out></td>
<td><c:out value="${emp.emailId}"></c:out></td>
<td><c:out value="${emp.cellNo}"></c:out></td>
<td><c:out value="${emp.birthDate}"></c:out></td>
<td><c:out value="${}"></c:out></td>
<td><c:out value="${emp.created}"></c:out></td>

<td colspan="2"></td>
<td colspan="2"><a href="${pageScope.myUrl}">Next Page</a></td>


After executing the above code, you will get the following page as output in the browser  :

Download Source Code


Share on Google+Share on Google+

Hibernate JSP

Posted on: March 31, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate JSP  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image