Struts Hibernate Integration

Struts hibernate integration is a common integration mechanism of the struts application. Hibernate is one of the powerful Object-Oriented mapping tool that maps the object view of data into relational database  for java and it includes efficient persistence services such as create, read, update and delete (CRUD). It also provides powerful, ultra-high performance OR persistence and Database query service for java web based applications. Normal strut hibernate integration architecture is given below.

Struts (Web page) <—> Spring DI <–> Hibernate (DAO) <—> Database

Here we can see how we can develop the high performance application by implementing the Struts and Hibernate all together in one system. The applications starts with the blank struts framework and we need to add the Hibernate library files into them. The additional and the important thing we need is the integration Plugin that should be defined in the struts-config.xml file. Steps for implementing strut hibernate integration.

1)      Create a new Hibernate Struts plug-in file to set the Hibernate session factory in servlet context, and include this file in struts-config.xml file.

2)      In Struts, get the Hibernate session factory from servlet context, and do whatever Hibernate task you want.

 

1)      Hibernate Struts Plug-in:- Create a Hibernate Struts Plug-in, to get the Hibernate session factory and store it into the servlet context for later user servlet.getServletContext().setAttribute(KEY_NAME, factory);

Eg: –

package com.j2eebrain.common.plugin;
import java.net.URL;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernatePlugin implements PlugIn {
private Configuration config;
private SessionFactory factory;
private String path = “/hibernate.cfg.xml”;
private static Class clazz = HibernatePlugin.class;
public static final String KEY_NAME = clazz.getName();
public void setPath(String path) {
this.path = path;
}
public void init(ActionServlet servlet, ModuleConfig modConfig)
throws ServletException {

try {
//save the Hibernate session factory into serlvet context
URL url = HibernatePlugin.class.getResource(path);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
servlet.getServletContext().setAttribute(KEY_NAME, factory);

} catch (MappingException e) {
throw new ServletException();
} catch (HibernateException e)  {
throw new ServletException();
}

}

public void destroy() {
try {
factory.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}

2)      Includes struts-config.xml:- Include the Hibernate Struts plug-in into the Struts configuration file (struts-config.xml).

Eg: –

<struts-config>
...
<plug-in className=“com.j2eebrain.common.plugin.HibernatePlugin”>
<set-property property=“path” value=“/hibernate.cfg.xml”/>
</plug-in>
....
<struts-config>

3)      To set the hibernate session factory: -In Struts action class, you can get the Hibernate session factory from servlet context.

Syntax: –

servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);

Eg: –

package com.j2eebrain.customer.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.j2eebrain.common.plugin.HibernatePlugin;
import com.j2eebrain.customer.form.CustomerForm;
import com.j2eebrain.customer.model.Customer;
public class AddCustomerAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
SessionFactory sessionFactory =
(SessionFactory) servlet.getServletContext()
.getAttribute(HibernatePlugin.KEY_NAME);
Session session = sessionFactory.openSession();
CustomerForm customerForm = (CustomerForm)form;
Customer customer = new Customer();
//copy customerform to model
BeanUtils.copyProperties(customer, customerForm);
//save it
customer.setCreatedDate(new Date());
session.beginTransaction();
session.save(customer);
session.getTransaction().commit();
return mapping.findForward(“success”);

}
}

Features of Hibernate plug-in integration with Struts

  1. Hibernate validator integration: – This feature allow the use of the fantastic Hibernate Validator Framework with very simple use of annotations in your Actions.
  2. Hibernate core sessions injection capability: – This feature provides an Interceptor that open and sets in the value stack a Hibernate Core Session and closes (or not) this after the results are rendered for the client.
  3. Hibernate transaction injection capability: – This feature provides an Interceptor that opens and sets in the value stack a Transaction and commit this after the results are rendered for the client if a rollback method was not invoked.
  4. Hibernate core configuration management web tool: – This feature provides a web front-end tool for view and reload your Hibernate Core configurations.
  5. Struts Spring Hibernate Integration is possible: – We can integrate spring and hibernate together. For this integration we need to integrate the following.

1)      Integrate spring with Hibernate with spring’s “LocalSessionFactoryBean” class.

2)      Integrate spring with Struts via spring’s ready make Struts plug-in – “ContextLoaderPlugIn”.