Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struts hiberanate integration tutorial part2
12-14-2011, 05:11 PM (This post was last modified: 01-06-2012 12:35 AM by admin.)
Post: #1
Thumbs Up 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

Quote:<?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

Quote:<!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

Quote:<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.
Find all posts by this user
Add Thank You Quote this message in a reply
01-06-2012, 03:32 PM (This post was last modified: 01-06-2012 03:33 PM by dada1.)
Post: #2
RE: Struts hiberanate integration tutorial part2
Thanks for the tutorial, let me check this.

Guys, To download the project. Just reply to this thread and click refresh. You will see the download link.
Find all posts by this user
Add Thank You Quote this message in a reply
01-07-2012, 10:27 PM
Post: #3
RE: Struts hiberanate integration tutorial part2
(01-06-2012 03:32 PM)dada1 Wrote:  Thanks for the tutorial, let me check this.

Guys, To download the project. Just reply to this thread and click refresh. You will see the download link.

Thanks for the heads up. This will help other members.
Find all posts by this user
Add Thank You Quote this message in a reply
01-08-2012, 11:40 PM
Post: #4
RE: Struts hiberanate integration tutorial part2
very useful for me
Find all posts by this user
Add Thank You Quote this message in a reply
01-12-2012, 09:38 PM
Post: #5
Wink RE: Struts hiberanate integration tutorial part2
(01-07-2012 10:27 PM)mjm Wrote:  
(01-06-2012 03:32 PM)dada1 Wrote:  Thanks for the tutorial, let me check this.

Guys, To download the project. Just reply to this thread and click refresh. You will see the download link.

Thanks for the heads up. This will help other members.
Find all posts by this user
Add Thank You Quote this message in a reply
01-15-2012, 06:07 PM
Post: #6
RE: Struts hiberanate integration tutorial part2
Thanks Rajasri for your nice post
Find all posts by this user
Add Thank You Quote this message in a reply
01-19-2012, 10:29 PM
Post: #7
any good please to sell and buy site traffic ?
hello there ..
it is my first post here and i hope that you will help me with it ..

any one could tell me about good please to sell and buy traffic ?

many thanks .
Visit this user's website Find all posts by this user
Add Thank You Quote this message in a reply
01-20-2012, 01:03 AM
Post: #8
RE: any good please to sell and buy site traffic ?
(01-19-2012 10:29 PM)SesShomssib Wrote:  hello there ..
it is my first post here and i hope that you will help me with it ..

any one could tell me about good please to sell and buy traffic ?

many thanks .

This forum is related to jobs and walkins.
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Login form authentication using Struts with database mjm 0 302 01-22-2012 02:52 AM
Last Post: mjm
Photo Simple Struts-Hibernate Integration tutorial to insert values rajasri 2 1,168 01-15-2012 06:10 PM
Last Post: mukesh1234
Sad Simple Struts Example rajasri 0 142 12-14-2011 03:47 PM
Last Post: rajasri
Tongue Struts Dispatch Action Tutorial with example rajasri 0 580 12-13-2011 01:26 AM
Last Post: rajasri
Photo How to export data as pdf in struts using displaytags rajasri 0 390 12-12-2011 11:08 PM
Last Post: rajasri
  Struts hiberanate integration tutorial part2 admin 0 142 11-29-2011 03:48 AM
Last Post: admin

Forum Jump:


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


WE will share important jobs and updates on facebook so like us to get updates