Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struts hiberanate integration tutorial part2
11-28-2011, 11:18 PM
Post: #1
Struts hiberanate integration tutorial part2
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]
<?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>
[/xml]
using this mapping file you can create required no of columns to your project
The hibernate configuration file is as follows
[xml]
<!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>
[/xml]
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 [xml]
<plug-in className="com.s2sgateway.HibernatePlugIn">
</plug-in>
[/xml]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
[java]

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");
}
}
[/java]
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.
Find all posts by this user
Add Thank You Quote this message in a reply
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
05-02-2012, 02:36 PM
Post: #2
RE: Struts hiberanate integration tutorial part2
Very useful post..
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Thumbs Up Struts hiberanate integration tutorial part2 rajasri 9 1,438 05-02-2012 04:29 PM
Last Post: admin
  Login form authentication using Struts with database mjm 1 1,973 04-27-2012 03:57 PM
Last Post: alexrabe
Photo Simple Struts-Hibernate Integration tutorial to insert values rajasri 2 3,539 01-15-2012 01:40 PM
Last Post: mukesh1234
Sad Simple Struts Example rajasri 0 397 12-14-2011 11:17 AM
Last Post: rajasri
Tongue Struts Dispatch Action Tutorial with example rajasri 0 1,387 12-12-2011 08:56 PM
Last Post: rajasri
Photo How to export data as pdf in struts using displaytags rajasri 0 1,018 12-12-2011 06:38 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)