3:35 pm - Monday August 18, 0921

Hibernate Struts Inserting Data Using Eclipse

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044


  • This application will use  join struts and hibernate applications.
  • Here First  we will  get input from front view html or Jsp page.
  • Then it will pass model and cotroller using struts application.
  • Then In controller page input will save the database using Hibernate application.
  • After save the inputs will pass the front view.

 Project Structure:


































  • Here we will use sessionFactory for create connetion Oracle database.
    • sessionFactory = new Configuration().configure


  • Then we will get input from AddController.java through arguments and then save the database using session.
    • stu.setStuname(stuname);
    • s.save(stu);
  • Then we will use  Transaction statement for  begin and commit the database.
    • Transaction tx= s.beginTransaction();
    • tx.commit();
package com.candidjava;

import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class AddStudentControl {

	private static SessionFactory sessionFactory;

	private Session getSession() {
		Session s = null;
		try {
			sessionFactory = new Configuration().configure(
			s = sessionFactory.openSession();
		} catch (HibernateException e) {
		return s;

	public void addNewStudent(String args, String args1, String args2)
			throws Exception {
		try {
			Session s = getSession();
			Transaction tx = s.beginTransaction();
			Student stu = new Student();

			System.out.println("Student name");
			String stuname = args;
			String studegree = args1;
			String stumobileno = args2;




			System.out.println("\n\n Details Added \n");
		} catch (HibernateException e) {
			System.out.println("error1" + e.getMessage());


}// end of class


  • It is a Controller class .It will extends Action class.
  • MyModel m = (MyModel)fm;//Here model class object will store in the variable name m.
  • String s=m.getName();String s1=m.getDegree();String s2=m.getMobileno();//Here get input from model class.
  • AddStudentControl ctrl = new AddStudentControl();//Here we will create object for AddStudentControl class
  • ctrl.addNewStudent(s,s1,s2);//Here pass the input to AddNewStudent method through Arguments from MyController class.
package com.candidjava;

import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyController extends Action {
	public ActionForward execute(ActionMapping map, ActionForm fm,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		HttpSession ses = req.getSession(true);

		MyModel m = (MyModel) fm;
		String s = m.getName();
		String s1 = m.getDegree();
		String s2 = m.getMobileno();


		AddStudentControl ctrl = new AddStudentControl();
		ctrl.addNewStudent(s, s1, s2);

		ses.setAttribute("s", s);
		ses.setAttribute("s1", s1);
		ses.setAttribute("s2", s2);
		if (s.equals("")) {
			return map.findForward("error");
		return map.findForward("success");



  • It is a Model class. It will extends ActionForm class which is  a Serializable class. Then here we will use get and set method.
    • private String name;
    • public String getName(){return name;}
    • public void setName(String n){name=n;      }
package com.candidjava;

import org.apache.struts.action.ActionForm;

public class MyModel extends ActionForm {
	private String name;
	private String degree;
	private String mobileno;

	public void setName(String n) {
		name = n;

	public String getName() {
		return name;

	public void setDegree(String n) {
		degree = n;

	public String getDegree() {
		return degree;

	public void setMobileno(String n) {
		mobileno = n;

	public String getMobileno() {
		return mobileno;



  • It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.
    • private long id;
    • public long getId() {return id;}
    • public void setId(long String) {id = String;}
package com.candidjava;

import java.io.*;

public class Student implements Serializable {
	private long id;
	private String stuname;
	private String studegree;
	private String stumobileno;

	public long getId() {
		return id;

	public String getStuname() {
		return stuname;

	public String getStudegree() {
		return studegree;

	public String getStumobileno() {
		return stumobileno;

	public void setId(long string) {
		id = string;

	public void setStuname(String string) {
		stuname = string;

	public void setStudegree(String string) {
		studegree = string;

	public void setStumobileno(String string) {
		stumobileno = string;




<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>//This tag will load the ActionServlet class.

  • <init-param><param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value> </init-param>//This tag will use configure the struts-config.xml.


  • <servlet-mapping><servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern> </servlet-mapping>//here *.do invoke the ActionServlet class.

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">






			<web-resource-name>My secure resources</web-resource-name>
			<description>Resources to be placed under security control.</description>


		<description>The role allowed to access our content</description>



<form-beans><form-bean type=”com.candidjava.MyModel” name =”model” />

</form-beans>//Beans tag will use for model class.Here name=”model” which is same as the controller name=”model”.

  • <action-mappings><action path=”/myActionForm” type=”com.candidjava.MyController” name=”model” input=”/Login.jsp”>//Action mapping tag will use both view and controller.Action tag will use for controller class.Here path=”/myActionForm” which is same as the front view Login.jsp action=”/myActionForm”.
  • <forward name=”success” path=”/Success.jsp” />//Forward tag will use for view page.It will forward  input to the view page
  • <forward name=”error” path=”/Fail.jsp” /></action></action-mappings>
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
		<form-bean type="com.candidjava.MyModel" name="model" />
		<action path="/myActionForm" type="com.candidjava.MyController"
			name="model" input="/Login.jsp">
			<forward name="success" path="/Success.jsp" />
			<forward name="error" path="/Fail.jsp" />



<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD//EN"
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
		<property name="connection.url">
		<property name="connection.username">
		<property name="connection.password">
		<property name="connection.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">false</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="com\\xml\\Student.hbm.xml" />


  • <class name=”com.candidjava.Student” table=”student” >//This tag will use for mapping Pojo class name and  table name.
  • <id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>//This tag will use for generating primary key id and also increment the id value.
  • <property name=”name” column=”name” not-null=”true”/>//This property will use for mapping the pojo class veriable name(name=”name” ) and  table attribute name(column=”name”).
  • </class>”/>//End of the class.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	<class name="com.candidjava.Student" table="StudentDet">
		<id name="id" type="long" column="Id">
			<generator class="increment" />
		<property name="stuname" column="StuName" not-null="true" />
		<property name="studegree" column="Degree" />
		<property name="stumobileno" column="MobileNo" />







Filed in: Z-A Hibernate codes

Comments are closed.