In this tutorial, I will guide you to integrate 2 power frameworks for Java EE application: Struts 2 and Hibernate 3 (I used the latest library version of them to the time I wrote this post). The example we will use is a simple blog, here is how it looks

We will develop some main features of a blogging application:
- To Create New Post
- To List All Posts in One Page
- To Manage Post: Edit/Remove Posts.
Software and Library needed:
- Eclipse Indigo 3.7
- Apache Tomcat 7.0
- MySQL 5
- MySQL Workbench
- Struts 2.2.3.1
- Hibernate Core 3.6.7
- slf4j-1.6.2
- apache-log4j-1.2.16
- JSTL 1.2
Note: All of these are the latest up to October 2011.
1. Setup Eclipse, Tomcat and New Struts-Hibernate Project Lib
In Eclipse, select Java EE perspective. 
In the Server view, right click and select New to create a new Instance of Tomcat 7 (if you don’t have one).
Now from Eclipse, right click in Project Explorer then select New > Web > Dynamic Web Project

Enter struts-hibernate-integration as the project name, select Tomcat 7 as the Runtime Server and you’ll be ready with a project structure like this.

Now you need to configure library for the application.

Please find the above jar file in these location:
- struts-2.2.3.1lib
- hibernate-distribution-3.6.7.Final
- hibernate-distribution-3.6.7.Finallibrequired
- hibernate-distribution-3.6.7.Finallibjpa
- slf4j-1.6.2
- apache-log4j-1.2.16
- jstl-1.2
Now if you collect all of the above jar files, please copy them all to the folder WebContent > WEB-INF > lib
2. Configure Struts 2 Application
Your next step is to configure Struts 2 Application and to make sure it will work.
First thing to do is to configure Struts 2 Filter in web.xml file (find this file under WebContent > WEB-INF folder. Configure it like this: web.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>struts-hibernate-integration</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create an XML file with name: struts.xml and put it under your src folder (to make sure it will visible in build folder when we deploy), with the following content.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/blog">
<action name="listBlog" method="listBlog"
class="com.googlecode.frameworksintegration.action.BlogAction">
<result name="success">/blog_listing.jsp</result>
</action>
<action name="newBlog" method="renderAddBlog"
class="com.googlecode.frameworksintegration.action.BlogAction">
<result name="success">/blog_adding.jsp</result>
</action>
<action name="addBlog" method="addBlog"
class="com.googlecode.frameworksintegration.action.BlogAction">
<result name="success">/notifications.jsp</result>
</action>
<action name="manageBlog" method="manageBlog"
class="com.googlecode.frameworksintegration.action.BlogAction">
<result name="success">/blog_manage.jsp</result>
</action>
<action name="deleteBlog" method="deleteBlog"
class="com.googlecode.frameworksintegration.action.BlogAction">
<result name="success">/notifications.jsp</result>
</action>
</package>
</struts>
You can see in the above struts config file that all the action we use in this example is point to BlogAction class, here is its content:
package com.googlecode.frameworksintegration.action;
import java.util.List;
import com.googlecode.frameworksintegration.dao.BlogDAO;
import com.googlecode.frameworksintegration.dao.BlogDAOImpl;
import com.googlecode.frameworksintegration.domain.Blog;
import com.opensymphony.xwork2.ActionSupport;
/**
* Struts 2 Integrated with Hibernate 3 blogging example.
* http://java.searchdaily.net
* @author namnvhue
*
*/
public class BlogAction extends ActionSupport {
private BlogDAO blogDAO = null;
private List<Blog> blogs;
private Blog blog;
private String message;
private String blogId;
/**
*
*/
public BlogAction() {
this.blogDAO = new BlogDAOImpl();
}
private static final long serialVersionUID = -4901097198092626247L;
public String listBlog() {
this.blogs = blogDAO.listBlogs();
return SUCCESS;
}
public String manageBlog() {
this.blogs = blogDAO.listBlogs();
return SUCCESS;
}
public String renderAddBlog() {
this.blog = new Blog();
return SUCCESS;
}
public String addBlog() {
blogDAO.save(this.blog);
this.message = "Blog has been created successfully.";
return SUCCESS;
}
public String deleteBlog() {
this.blog = this.blogDAO.find(Integer.parseInt(blogId));
this.blogDAO.delete(blog);
this.message = "Blog has been removed successfully.";
return SUCCESS;
}
public String editBlog() {
return SUCCESS;
}
public String detail(String id) {
return SUCCESS;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
public Blog getBlog() {
return blog;
}
public void setBlog(Blog blog) {
this.blog = blog;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getBlogId() {
return blogId;
}
public void setBlogId(String blogId) {
this.blogId = blogId;
}
}
If you enter all the above code now, Eclipse will give you some compiling error, but don’t worry, we will fix it softly.
3. Prepare MySQL database and Configure JPA persistence unit
We will use MySQL 5 as the database and JPA over Hibernate 3, first thing to do is to create a new schema named blog and create a blog table with the following structure:
CREATE TABLE `blog` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`CONTENT` text,
`POSTED_BY` varchar(255) DEFAULT NULL,
`POSTED_DATE` datetime DEFAULT NULL,
`TITLE` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
Now in the src folder of the project, create a folder name: META-INF and create inside it a file name: persistence.xml.
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="blogds">
<class>com.googlecode.frameworksintegration.domain.Blog</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/blog" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password" value="mysql_password" />
<property name="hibernate.connection.username" value="mysql_user" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
4. Configure Hibernate and DAO layer for Struts Application.
Now you will see how to configure an entity for using Hibernate. Here is the Blog class you see as the domain object in BlogAction action before.
package com.googlecode.frameworksintegration.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Struts 2 Integrated with Hibernate 3 blogging example.
* http://java.searchdaily.net
* @author namnvhue
*
*/
@Entity
@Table(name = "BLOG")
public class Blog {
private Integer id;
private String title;
private String content;
private Date postedDate;
private String postedBy;
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "CONTENT")
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Column(name = "POSTED_DATE")
@Temporal(TemporalType.TIMESTAMP)
public Date getPostedDate() {
return postedDate;
}
public void setPostedDate(Date postedDate) {
this.postedDate = postedDate;
}
@Column(name = "POSTED_BY")
public String getPostedBy() {
return postedBy;
}
public void setPostedBy(String postedBy) {
this.postedBy = postedBy;
}
@Column(name = "TITLE")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
The blogDAO interface:
package com.googlecode.frameworksintegration.dao;
import java.util.List;
import com.googlecode.frameworksintegration.domain.Blog;
/**
* Struts 2 Integrated with Hibernate 3 blogging example.
* http://java.searchdaily.net
*
* @author namnvhue
*
*/
public interface BlogDAO {
public Blog find(Integer id);
public List listBlogs();
public void save(Blog blog);
public void delete(Blog blog);
}
The implementation of blogDAO using Hibernate
package com.googlecode.frameworksintegration.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.googlecode.frameworksintegration.domain.Blog;
/**
* Struts 2 Integrated with Hibernate 3 blogging example.
* http://java.searchdaily.net
* @author namnvhue
*
*/
public class BlogDAOImpl implements BlogDAO {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
public BlogDAOImpl() {
this.entityManagerFactory = Persistence
.createEntityManagerFactory("blogds");
this.entityManager = entityManagerFactory.createEntityManager();
}
@SuppressWarnings("unchecked")
@Override
public List listBlogs() {
Query q = entityManager.createQuery("select blog from Blog blog");
return q.getResultList();
}
@Override
public void save(Blog blog) {
entityManager.getTransaction().begin();
entityManager.persist(blog);
entityManager.flush();
entityManager.getTransaction().commit();
}
@Override
public void delete(Blog blog) {
entityManager.getTransaction().begin();
entityManager.remove(blog);
entityManager.flush();
entityManager.getTransaction().commit();
}
public EntityManager getEntityManager() {
return entityManager;
}
@Override
public Blog find(Integer id) {
return entityManager.find(Blog.class, id);
}
}
You won’t see any import from Hibernate here, because we use Hibernate as a Persistence Provider to JPA and for a standard rule, more and more projects are using it this way.