April 19, 2024

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
git clone https://gitlab.com/giuseppeurso-eu/struts-mvc

STEP 1.

Create a database sample schema:

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

 STEP 2.

Download and unzip the Hibernate 3.3.2 release bundle:  http://sourceforge.net/projects/hibernate/files/hibernate3/

hibernate-unzipped

STEP 3.

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: http://dev.mysql.com/downloads/connector/j/

STEP 5.

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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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>
</session-factory>
</hibernate-configuration>

 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 java.io.IOException;
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 {
sf.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sf.getCurrentSession().getTransaction().commit();
} catch (StaleObjectStateException staleEx) {
throw staleEx;
} catch (Throwable ex) {
// Rollback only
ex.printStackTrace();
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
System.out.print(rbEx.getStackTrace());
}
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 -->
<filter>
<filter-name>HibernateSessionFilter</filter-name>
<filter-class>com.demo.hibernate.SessionRequestFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>HibernateSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

The final project layout should look like this

hibernate-base-project-layout

 STEP 9.

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

hibernate-build-session-factory

Related posts

Leave a Reply

Your email address will not be published.