Today i am going to give you a complete working struts-hibernate integration project.To see the part1 struts-hibernate tutorial click here
This project uses the hibernate plugin to create the session factory and uses hibernate mapping file hibernate.hbm.xml to create the necessary table values
The hibernte mapping xml file contains the below values
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="com.s2sgateway.Marks" table="marks">
<id name="id" type="java.lang.Integer" column="Id" unsaved-value="null">
<generator class="native" />
</id>
<property
name="last_name" type="java.lang.String"
column="Last_name" not-null="true" length="100"/>
<propertyname="first_name" type="java.lang.String"
column="First_name"not-null="true"length="100"/>
<property name="subject1" type="java.lang.Integer"
column="subject1" not-null="true"/>
<property name="subject2" type="java.lang.Integer"
column="subject2" not-null="true"/>
<property name="subject3" type="java.lang.Integer"
column="subject3" not-null="true"/>
<property name="total" type="java.lang.Integer"
column="Total" not-null="true"/>
</class>
</hibernate-mapping>
using this mapping file you can create required no of columns to your project
The hibernate configuration file is as follows
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/s2sgateway</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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="/com/s2sgateway/Marks.hbm.xml"/>
</session-factory>
</hibernate-configuration>
In this file the mapping file marks is mapped as resource to create the table and the hibernate.hb.2ddl.auto is set to update so table once created then values is updated thereafter
The action file contains the code needed to call the session factory from the hibernate plugin for hibernate plugin to work simply add these statement in struts-config.xml file
<plug-in className="com.s2sgateway.HibernatePlugIn">
</plug-in>
The hibernate plugin file is included in example project you are about to download at the end of this tutorial.The plugin create the mapping and opens the session from there values are get and marks are added
package com.s2sgateway;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.util.List;
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 com.s2sgateway.HibernatePlugIn;
import com.s2sgateway.Marks;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
public class AddMarksPageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
AddMarksPageActionForm formObj = (AddMarksPageActionForm)form;
ServletContext context = request.getSession().getServletContext();
/*Retrieve Session Factory */
SessionFactory _factory = (SessionFactory)
context.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);
/*Open Hibernate Session */
Session session = _factory.openSession();
try {
//Inserting Marks to Table
Marks marks = new Marks();
marks.setFirst_name(formObj.getFirstname());
marks.setLast_name(formObj.getLastname());
marks.setEnglish(formObj.getEnglish());
marks.setMaths(formObj.getMaths());
marks.setPhysics(formObj.getPhysics());
marks.setTotal(new Integer(formObj.getEnglish().intValue()
+formObj.getMaths().intValue()+formObj.getPhysics().intValue()));
session.beginTransaction();
session.save(marks);
session.getTransaction().commit();
} catch(Exception e) {
session.getTransaction().rollback();
}
//Fetching result list
List result=session.createQuery("from Marks order by total desc").list();
request.setAttribute("result",result);
/*Close session */
session.close();
return mapping.findForward("success");
}
}
The index file get the values in the struts-html.tld,html:text box and call the struts-config.xml action and then it is transferred to action file to process the values and display the values in the same page.
Download the complete Project Here
strutshibernates2sgateway
Any doubt ask us by comments.