Spring MVC Tutorial – Paging Through Hibernate and Selection

In this installment, I’m expanding the project done in Spring MVC Tutorial – Hibernate Integration to include:

  1. Browsing/paging through a table with a navigation bar found in various forum sites and ASP.NET GridView (e.g. First 3 4 5 6 7 Last)
  2. Showing checkboxes against each row of the table for selective action

The related project code download is ibank-v2.zip

Handling Navigation/Pagination/Paging

There are free libraries out there like Google’s Jmesa, DisplayTag, etc. and also Spring has support through PagedListHolder and such. But I was in a different mood and decided to go for my own implementation.

Also, this helped me understand the logics of displaying a navigation bar. Maybe I’ll learn about the libraries later and share with you.


This is the workhorse for the navigation bar:

package org.himu.ibank.service.vo;

public class NavigationInfo {

private int currentPage;
private int pageSize;
private int rowCount;
private int maxIndices;

public NavigationInfo() {
currentPage = 0;
rowCount = 0;
maxIndices= 5;
pageSize = 5;

public int getCurrentPage() {
return currentPage;

public void setCurrentPage(int currentPage) {
if (currentPage < 0)
this.currentPage = 0;
else if (currentPage > getPageCount() – 1)
this.currentPage = getPageCount() – 1;
this.currentPage = currentPage;

public int getPageSize() {
return pageSize;

public void setPageSize(int pageSize) {
this.pageSize = pageSize;

public int getRowCount() {
return rowCount;

public void setRowCount(int rowCount) {
this.rowCount = rowCount;

public int getMaxIndices() {
return maxIndices;

public void setMaxIndices(int maxIndices) {
this.maxIndices = maxIndices;

public int getPageCount() {
return (int) Math.ceil((double) rowCount / pageSize);

public int getPrevIndex() {
int prev = currentPage – 1;
return prev < 0 ? 0 : prev;

public int getNextIndex() {
int lastIndex = getPageCount() – 1;
int next = currentPage + 1;
return next > lastIndex ? lastIndex : next;
public boolean isFirstPage() {
return 0 == currentPage;
public boolean isLastPage() {
return (getPageCount() – 1) == currentPage;

public int[] getIndexList() {
int[] range = getIndexRange();
int[] ilist = new int[range[1] – range[0] + 1];
for (int i = 0; i < ilist.length; i++) {
ilist[i] = range[0] + i;
return ilist;
public int[] getIndexRange() {
// determine the standard window
int start = currentPage – maxIndices / 2;
int end = start + maxIndices – 1;
// shift to right if start underflows 0
if (start < 0) {
end -= start; // end – -start = end + start = shift right
start = 0;
// now maybe the window overflows pageCount – so shift to left again
int lastIndex = getPageCount() – 1;
if (end > (lastIndex)) {
start -= (end – lastIndex);
end = lastIndex;
// we have finalized end, now if start < 0 then truncate it
if (start < 0)
start = 0;
return new int[] {start, end};

The page indices are 0-based. The class has some defaults which can be overridden if required.

The starting point for this class is setRowCount(). Once the number of rows is determined you can use getPageCount() which in turn allows all other navigation methods to work.

setCurrentPage(), getPrevIndex() and getNextIndex() make sure you don’t fall out of valid page indices.

getIndexRange() is where all the muscle is. It uses currentPage and maxIndices (maximum number of links to be displayed in the navigation bar) to determine the start and end of page links in the navigation bar.

getIndexList() is a convenience method for generating the entire list of navigation indices from the range calculated by getIndexRange().


This class is used to send the navigation information and the current page of customers to the view.

package org.himu.ibank.service.vo;

import java.util.List;

import org.himu.ibank.domain.Customer;

* Hold necessary information for paged view of customer list
public class PagedCustView {

private NavigationInfo navInfo = new NavigationInfo();
private List<Customer> customers;

public NavigationInfo getNavInfo() {
return navInfo;

public void setNavInfo(NavigationInfo navInfo) {
this.navInfo = navInfo;

public List<Customer> getCustomers() {
return customers;

public void setCustomers(List<Customer> customers) {
this.customers = customers;

public Customer getCustomer(int i) {
return (Customer) customers.get(i);

public void setCustomer(int i, Customer customer) {
this.customers.add(i, customer);

The actual page of customers is fetched in the controller and the navigation information is set accordingly.

Writing the Controller

You’re going to browse all registered but unauthorized customers.

The NewCustListController expects a page parameter in the query string of its calling URL. If this is not found then page 0 (the first page) is assumed.

package org.himu.ibank.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.himu.ibank.service.CustomerRegistrationService;
import org.himu.ibank.service.vo.PagedCustView;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class NewCustListController extends AbstractController {

private CustomerRegistrationService custRegService;
public void setCustRegService(CustomerRegistrationService custRegService) {
this.custRegService = custRegService;

protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<Object, Object> model = new HashMap<Object, Object>();
PagedCustView pcustv = new PagedCustView();


String page = (String)request.getParameter("page");
if (null == page)

pcustv.getNavInfo().getCurrentPage(), pcustv.getNavInfo().getPageSize()));

request.getSession().setAttribute("pagedcust", pcustv);
model.put("pagedcust", pcustv);

return new ModelAndView("admin/newcustlist", model);

  1. First you determine the customer count and tell it to NavigationInfo.
  2. Then you set the current page.
  3. Based on the current page and page size as specified in NavigationInfo, you then fetch the only the specific page of customers through Hibernate. This is the reason I opted for my own implementation.
  4. Return the model and view name as usual.

The Service and DAO Classes

I’ve refactored CustomerRegistrationService and CustomerDao interfaces and their implementations a bit. Also note that I’ve corrected a mistake in HibernateCustomerDao’s paging code.


public interface CustomerRegistrationService {

Long registerCustomer(Customer c);
void authorizeCustomer(Customer c);
Customer getCustomer(Long id);
List<Customer> getUnauthorizedCustomers(int startPage, int pageSize);
List<Customer> getCustomers(int startPage, int pageSize);
List<Customer> getAllCustomers(int startPage, int pageSize);
public int getUnauthorizedCustomerCount();


public class StandardCustomerRegistrationService implements CustomerRegistrationService { private CustomerDao custDao; public void setCustDao(CustomerDao custDao) { this.custDao = custDao; } @Override public Long registerCustomer(Customer c) { return custDao.addNew(c); } @Override public void authorizeCustomer(Customer c) { if (c.getStatus() == Customer.STATUS_REGISTERED) { c.setStatus(Customer.STATUS_ACTIVE); custDao.update(c); // … mailer code for notifying customer … } } @Override public Customer getCustomer(Long id) { return custDao.findById(id); } /** * Get registered but unauthorized customers using optional paging. * * @param startPage Starting page number, first page is 0 * @param pageSize Size of a single page, <=0 means no paging */ @Override public List<Customer> getUnauthorizedCustomers(int startPage, int pageSize) { Customer c = new Customer(); c.setStatus(Customer.STATUS_REGISTERED); return custDao.listByExample(c, startPage, pageSize); } @Override public int getUnauthorizedCustomerCount() { return custDao.countCustomers(Customer.STATUS_REGISTERED); } /** * Get registered and authorized customers using optional paging. * * @param startPage Starting page number, first page is 0 * @param pageSize Size of a single page, <=0 means no paging */ @Override public List<Customer> getCustomers(int startPage, int pageSize) { Customer c = new Customer(); c.setStatus(Customer.STATUS_ACTIVE); return custDao.listByExample(c, startPage, pageSize); } @Override public List<Customer> getAllCustomers(int startPage, int pageSize) { return custDao.listAll(startPage, pageSize); } }


public interface CustomerDao {

Long addNew(Customer c);
void delete(Customer c);
void update(Customer c);
Customer findById(Long id);
Customer findByUserId(String uid);
List<Customer> listAll();
List<Customer> listAll(int startPage, int pageSize);
List<Customer> listByExample(Customer c);
List<Customer> listByExample(Customer c, int startPage, int pageSize);
int countCustomers(int status);


public class HibernateCustomerDao extends HibernateDaoSupport implements
CustomerDao {

public Long addNew(Customer c) {
return (Long) getHibernateTemplate().save(c);

public void delete(Customer c) {

public void update(Customer c) {

public Customer findById(Long id) {
return (Customer) getHibernateTemplate().get(Customer.class, id);

public Customer findByUserId(String uid) {
List<Customer> clist =  getHibernateTemplate().find("from Customer c where c.userId = ?", uid);
if (clist.isEmpty())
return null;
return clist.get(0);

public List<Customer> listAll() {
return getHibernateTemplate().find("from Customer c");

public List<Customer> listAll(int startPage, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
return getHibernateTemplate().findByCriteria(criteria, startPage * pageSize, pageSize);

public List<Customer> listByExample(Customer c) {
return getHibernateTemplate().findByExample(c);

public List<Customer> listByExample(Customer c, int startPage, int pageSize) {
return getHibernateTemplate().findByExample(c, startPage * pageSize, pageSize);

public int countCustomers(int status) {
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class)
.add(Restrictions.eq("status", status))
return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);

The Browsing JSP

The Model and Controller codes are done. Now you write the view – newcustlist.jsp. Create it by copying admin.jsp as usual. Following is the relevant additions you need to write:
<div id="content"> <div align="center"> <c:if test="${!pagedcust.navInfo.firstPage}"> <a href="newcustlist.htm?page=0">First</a>&nbsp; </c:if> <c:forEach var="i" items="${pagedcust.navInfo.indexList}"> <c:choose> <c:when test="${i != pagedcust.navInfo.currentPage}"> <a href="newcustlist.htm?page=${i}">${i}</a>&nbsp; </c:when> <c:otherwise> <b>${i}</b>&nbsp; </c:otherwise> </c:choose> </c:forEach> <c:if test="${!pagedcust.navInfo.lastPage}"> <a href="newcustlist.htm?page=${pagedcust.navInfo.pageCount – 1}">Last</a> </c:if> </div> <form action="<%=request.getContextPath()%>/authorizecust.htm" method="post"> <table> <thead> <tr> <td>User ID</td> <td>First Name</td> <td>Last Name</td> <td>Email</td> </tr> </thead> <tbody> <c:forEach var="cust" items="${pagedcust.customers}"> <tr> <td>${cust.userId}</td> <td>${cust.firstName}</td> <td>${cust.lastName}</td> <td>${cust.email}</td> <td><input type="checkbox" value="${cust.id}" name="authorized"/></td> </tr> </c:forEach> </tbody> </table> <input type="hidden" name="linkBackIndex" value="${pagedcust.navInfo.currentPage}"/> <input type="submit" value="Authorize"/> </form> </div>
  1. First, generate the navigation banner aligned in the center of the page. It shows optional First and Last links based on the current page. The center <c:forEach> loops through the index list taken from navInfo and generates the necessary page links excluding the current page.
  2. Declare a form which allows us to submit authorization approvals of selected customers. The code for authorization will be written later in another controller.
  3. Looping through the customer list is trivial. The important thing is generating a checkbox against each customer for selecting for approval. The name of the checkboxes must be same (authorize in the above code) but there values will be different – the internal ID of each customer. This ID is required for Hibernate.
  4. A hidden input field linkBackIndex is also declared which helps us to return to the current page after authorization is made. It will be clear in the authorization controller.

Processing Actual Authorization and Returning to the New Customer Browser

First, the code:

package org.himu.ibank.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.himu.ibank.domain.Customer;
import org.himu.ibank.service.CustomerRegistrationService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

public class AuthorizeCustController extends AbstractController {

private CustomerRegistrationService custRegService;

public void setCustRegService(CustomerRegistrationService custRegService) {
this.custRegService = custRegService;

protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String[] custids = request.getParameterValues("authorized");
String linkBackIndex = request.getParameter("linkBackIndex");
System.out.println("################### INSIDE AuthorizeCustController #############");
for (String custid: custids) {
Customer cust = custRegService.getCustomer(Long.parseLong(custid));
System.out.println("## Authorized: " + custid);
return new ModelAndView(new RedirectView(request.getContextPath() + "/newcustlist.htm?page=" + linkBackIndex));

The logic is simple:

  1. Get the list of selected customer IDs using request.getParameterValues(“authorized”). It will contain only those IDs whose checkboxes are marked.
  2. Iterate through the list as ask our service class to make the authorizations.
  3. We use the linkBackIndex to redirect the flow to NewCustListController using its Spring MVC defined URL, i.e. /newcustlist.htm.

Notice how we are redirecting in the return statement. The RedirectView class helps us to go to back to the new customer browser and also pass the last page used as a query parameter. An alternative is to define the view in the bean configuration file using the pattern ‘redirect:<url>’, e.g. ‘redirect:/newcustlist.htm’ but we cannot pass the query parameter then.

Modified ibank-servlet.xml

Finally, our gluing file. I won’t be discussing it as there is nothing special.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="simpleUrlMapping">
<property name="mappings">
<prop key="/home.htm">homePageController</prop>
<prop key="/adminhome.htm">adminHomePageController</prop>
<prop key="/createcust.htm">newCustomerController</prop>
<prop key="/newcustlist.htm">newCustomerListController</prop>
<prop key="/authorizecust.htm">authorizeCustController</prop>
<bean id="viewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<bean id="homePageController"/>
<bean id="adminHomePageController">
<property name="viewName" value="admin/admin"/>
<bean id="newCustomerController">
<property name="custRegService" ref="customerRegService"/>
<property name="formView" value="admin/newcustomer"/>
<property name="successView" value="admin/newcustomer-success"/>
<property name="commandName" value="customer"/>
<property name="commandClass" value="org.himu.ibank.domain.Customer"/>
<bean id="newCustomerListController">
<property name="custRegService" ref="customerRegService"/>
<bean id="authorizeCustController">
<property name="custRegService" ref="customerRegService"/>

Hope to hear from you.

Running liferay behind Apache2 mod_jk and Tomcat6 on Debian

I cant figure out what i need to do to configure apache2 to talk to tomcat on my Debian linux environment. I have liferay running on Tomcat which i can access via the following URL


I am trying to configure Apache2 using mod_jk to allow me to access liferay using something like but everything i do just returns the following error

2The requested URL /liferay was not found on this server.
3Apache/2.2.9 (Debian) mod_jk/1.2.26 PHP/5.2.6-1+lenny3 with Suhosin-Patch Server at Port 80

Here is my configuration




2hpmedia:/etc/apache2/sites-available# cat liferay
3<VirtualHost *:80>
4 ServerAdmin webmaster@localhost
5 JkMount /liferay default
6 DirectoryIndex index.php index.html
8 ErrorLog /var/log/apache2/error.log
10 # Possible values include: debug, info, notice, warn, error, crit,
11 # alert, emerg.
12 LogLevel warn

There is a symbolik link in sites-enabled that points to the above file.

changes to /etc/apache2/apache2.conf

2JkWorkersFile /data/resourceRepository/internal/workarea/liferay/liferay_home/apache-tomcat-6.0.20/conf/workers.properties
4JkLogFile /data/resourceRepository/internal/workarea/liferay/liferay_home/apache-tomcat-6.0.20/logs/jk.log
6JkLogLevel info
8JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”
10JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
12JkRequestLogFormat “%w %V %T”


2LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so

My environment is as follows

Debian 5.2
Tomcat 6
Liferay 5.2.3

Is there anything i am doing wrong in the above configuration?

here are my virtual hosts files in /etc/apache2/sites-enabled

2<VirtualHost *:80>
3 ServerAdmin webmaster@localhost
5 DocumentRoot /data/resourceRepository/internal/workarea/joomla/bravaonline
6 <Directory />
7 Options FollowSymLinks
8 AllowOverride None
9 </Directory>
10 <Directory /data/resourceRepository/internal/workarea/joomal/bravaonline/>
11 Options Indexes FollowSymLinks MultiViews
12 AllowOverride None
13 Order allow,deny
14 allow from all
15 </Directory>
17 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
18 <Directory “/usr/lib/cgi-bin”>
19 AllowOverride None
20 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
21 Order allow,deny
22 Allow from all
23 </Directory>
25 ErrorLog /var/log/apache2/error.log
27 # Possible values include: debug, info, notice, warn, error, crit,
28 # alert, emerg.
29 LogLevel warn
31 CustomLog /var/log/apache2/access.log combined
33 Alias /doc/ “/usr/share/doc/”
34 <Directory “/usr/share/doc/”>
35 Options Indexes MultiViews FollowSymLinks
36 AllowOverride None
37 Order deny,allow
38 Deny from all
39 Allow from ::1/128
40 </Directory>

and the second one which i want to point to the tomcat installation is

2<VirtualHost www.brava1.com>
3 ServerAdmin webmaster@localhost
4 ServerName www.brava1.com
5 ServerAlias brava1.com
7 JkMount /lifer* default
8 DirectoryIndex index.php index.html
10 ErrorLog /var/log/apache2/error.log
12 # Possible values include: debug, info, notice, warn, error, crit,
13 # alert, emerg.
14 LogLevel warn

i changed it slightly to try and access it using http://www.brava1.com/liferay but i still get the same problem.

I setup a similar environment using these steps:

Setting up Freemarker + Spring Portlet in Spring MVC – Spring

I had been searching and searching and to no avail.
Can some one please show me how to set up spring portlet to use Freemarker instead of jsp/jstl in an spring mvc framework please??
Havent had much luck finding it on the web

well specifically want to know whats the portlet equivilant of this
<bean id=”urlMapping”>
<property name=”mappings”>
<property name=”order” value=”0″/>

given that there is no SimpleUrlHandlerMapping in the web.portlet packages in spring.

Okay. That doesn’t have anything to do with JSP / FreeMarker. Just a straightforward “how does Spring MVC map portlet requests to controllers/views?” thing.

Are you using Spring 2.5 or an older version? Reason I ask is, the first choice is whether you are going to use the Annotation-based Controllers or the Interface-based Controllers. Mapping is quite different depending on these. If you are using 2.5, I highly recommend going with the Annotation-based controllers.

Other resources to look at to understand the major differences between servlet requests and portlet requests in Spring:

Chapter 16 of the reference manual:

The slides and sample code from a seminar I did earlier this year on Spring Portlet MVC:

Hibernate LazyInitializer error in tomcat



caused by:
org.apache.jasper.JasperException: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer;
caused by:
javax.servlet.ServletException: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer;
caused by:
java.lang.NoSuchMethodError: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer;

After using the web client for a few minutes.


   I keep getting this error: javax.faces.FacesException: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer; caused by: org.apache.jasper.JasperException: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer; caused by: javax.servlet.ServletException: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer; caused by: java.lang.NoSuchMethodError: java.lang.Object.getHibernateLazyInitializer()Lorg/hibernate/proxy/LazyInitializer; After using the web client for a few minutes.

This exception is usually due to the JVM not being run with the -server option. The startup.bat/sh scripts set up the appropriate JAVA_OPTS.


How to set JNDI in Hibernate?

How to set JNDI in Hibernate?

Configuration for Tomcat and Jboss is almost same.

I did check in both it is working.

Set the following in Hibernate.hbm and then set the datasource in jndi config file.

<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD//EN”

<!– a SessionFactory instance listed as /jndi/name –>

<!– properties –>
<property name=”connection.datasource”>java:/comp/env/jdbc/MyDB</property>
<property name=”dialect”>org.hibernate.dialect.SCJASQLDialect</property>
<property name=”show_sql”>true</property>
<property name=”transaction.factory_class”>
<property name=”jta.UserTransaction”>java:comp/UserTransaction</property>

<!– mapping files –>

<!– cache settings –>



server.xml configuration

Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to $CATALINA_HOME/conf/server.xml.

Add this in between the </Context> tag of the examples context and the </Host> tag closing the localhost definition.

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">
  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_DBTest_log." suffix=".txt"
  <Resource name="jdbc/MyDB"
  <ResourceParams name="jdbc/MyDB">
    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
    <!-- MySQL dB username and password for dB connections  -->
    <!-- Class name for mm.mysql JDBC driver -->
    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.

Hibernate Dielect example

Hibernate Dielect example

<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD//EN”

<property name=”hibernate.connection.driver_class”>

<property name=”hibernate.connection.url”>

<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”></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.hbm2ddl.auto”>update</property>
<!– Mapping files –>
<mapping resource=”contact.hbm.xml“/>

In the above configuration file we specified to use the “hibernatetutorial” which is running on localhost and the user of the database is root with no password. The dialect property is org.hibernate.dialect.MySQLDialect which tells the Hibernate that we are using MySQL Database. Hibernate supports many database. With the use of the Hibernate (Object/Relational Mapping and Transparent Object Persistence for Java and SQL Databases), we can use the following databases dialect type property:

  • DB2- org.hibernate.dialect.DB2Dialect
  • HypersonicSQL – org.hibernate.dialect.HSQLDialect
  • Informix – org.hibernate.dialect.InformixDialect
  • Ingres – org.hibernate.dialect.IngresDialect
  • Interbase – org.hibernate.dialect.InterbaseDialect
  • Pointbase – org.hibernate.dialect.PointbaseDialect
  • PostgreSQL – org.hibernate.dialect.PostgreSQLDialect
  • Mckoi SQL – org.hibernate.dialect.MckoiDialect
  • Microsoft SQL Server – org.hibernate.dialect.SQLServerDialect
  • MySQL – org.hibernate.dialect.MySQLDialect
  • Oracle (any version) – org.hibernate.dialect.OracleDialect
  • Oracle 9 – org.hibernate.dialect.Oracle9Dialect
  • Progress – org.hibernate.dialect.ProgressDialect
  • FrontBase – org.hibernate.dialect.FrontbaseDialect
  • SAP DB – org.hibernate.dialect.SAPDBDialect
  • ass=”klink”>Sybaseorg.hibernate.dialect.SybaseDialect
  • Sybase Anywhere – org.hibernate.dialect.SybaseAnywhereDialect

The <mapping resource=”contact.hbm.xml“/> property is the mapping for our contact table.

Writing First Persistence Class
Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column. Here is the code for Contact.java:

package com.hibernate;

public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

* @return Email
public String getEmail() {
return email;

* @return First Name
public String getFirstName() {
return firstName;

* @return Last name
public String getLastName() {
return lastName;

="color:white;"> /**
* @param string Sets the Email
public void setEmail(String string) {
email = string;

* @param string Sets the First Name
public void setFirstName(String string) {
firstName = string;

* @param string sets the Last Name
public void setLastName(String string) {
lastName = string;

* @return ID Returns ID
public long getId() {
return id;

* @param l Sets the ID
public void setId(long l) {
id = l;


Mapping the Contact Object to the Database Contact table
The file contact.hbm.xml is used to map Contact Object to the Contact
table in the database. Here is the code for contact.hbm.xml:

<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”

<class name=”example.hibernate.Contact” table=”CONTACT”>
<id name=”id” type=”long” column=”ID” >
<generator class=”assigned”/>

<property name=”firstName”>
<column name=”FIRSTNAME” />
<property name=”lastName”>
<column name=”LASTNAME”/>
<property name=”email”>
<column name=”EMAIL”/>

Setting Up MySQL Database
In the configuration file(hibernate.cfg.xml) we have specified to use hibernatetutorial database running on localhost. So, create the databse (“hibernatetutorial”) on the MySQL server running on localhost.

Developing Code to Test Hibernate example
Now we are ready to write a program to insert the data into database. We should first understand about the Hibernate’s Session. Hibernate Session is the main runtime interface between a Java application and Hibernate. First we are required to get the Hibernate Session.SessionFactory allows application to create the Hibernate Sesssion by reading the configuration from hibernate.cfg.xml file. Then the save method on session object is used to save the contact information to the database:


Here is the code of FirstExample.java

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {
public static void main(String[] args) {
Session session = null;

// This step will read hibernate.cfg.xml

and prepare hibernate for use
SessionFactory sessionFactory = new

session =sessionFactory.openSession();
//Create new instance of Contact and set

values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
}catch(Exception e){
// Actual contact insertion will happen at this step