August 24, 2019

Hibernate and Struts base configuration

Hibernate is a powerfull Object Relationl Mapping java-based framework. It consists of a collection of related projects enabling developers to utilize POJO-style domain models in their applications in ways extending well beyond Object/Relational. In this article I will create a Hibernate ORM base configuration for a Struts MVC web application. Suppose you have already created a web dynamic java project using the Eclipse IDE (take a look here). For this example I will use a Mysql database connector driver.
Let’s see now the main steps to configure hibernate in a java struts web project.

SOURCE CODE (/giuseu/struts-mvc)

git clone


Create a database sample schema:

$ mysql -uroot -proot
mysql> create database demodb;

 STEP 2.

Download and unzip the Hibernate 3.3.2 release bundle:



Copy the following jar libraries into the project WebContent/WEB-INF/lib directory:

- antlr-*.jar
- commons-collections-*.jar
- dom4j-*.jar
- jta-*.jar
- javassist-*.jar
- slf4j-api-*.jar
- slf4j-jcl-*.jar
- hibernate*.jar

 STEP 4.

Download the Mysql JDBC Driver and copy the jar file in the lib directory project:


Create the hibernate.cfg.xml hibernate configuration file in the “src” project directory:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/demodb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">1</property>

 STEP 6.

Create a utility class to build the hibernate SessionFactory from hibernate.cfg.xml

package com.demo.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class SessionUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory() {
return sessionFactory;

 STEP 7.

Create a servlet filter to check the hibernate SessionFactory

package com.demo.hibernate;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;

public class SessionRequestFilter implements Filter {

private SessionFactory sf;

public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (StaleObjectStateException staleEx) {
throw staleEx;
} catch (Throwable ex) {
// Rollback only
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
} catch (Throwable rbEx) {
throw new ServletException(ex);

public void init(FilterConfig filterConfig) throws ServletException {
sf = SessionUtil.getSessionFactory();

public void destroy() {}


 STEP 8.

Declare the servlet filter in the web.xml file:

<!-- Filter Configuration -->


The final project layout should look like this


 STEP 9.

Start Tomcat in Eclipse, if not errors occur you can see the following INFO messages:


Related posts

Leave a Reply

Your email address will not be published.