Struts2 Hibernate Example With XML | Code Factory

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form action="insertData" method="post">
First Name :
<input type="text" name="firstName">
<br> Last Name :
<input type="text" name="lastName">
<input type="submit" value="Submit">
<?xml version="1.0" encoding="UTF-8"?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<constant name="struts.devMode" value="true" />
<include file="struts-default.xml" />
<package name="HibernateWithXML" extends="struts-default,json-default">
<action name="insertData" class="com.codeFactory.action.StoreData">
<result name="success">index.jsp</result>
<result name="error">index.jsp</result>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">
package com.codeFactory.action;import org.hibernate.Session;
import org.hibernate.Transaction;
import com.codeFactory.bean.Employee;
import com.codeFactory.connection.HibernateConnection;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class StoreData extends ActionSupport implements ModelDriven < Employee > {private Employee emp = new Employee();
HibernateConnection hbmCon = new HibernateConnection();
public String execute()
Session session1 = hbmCon.getHbmConnection();
Transaction t = session1.beginTransaction();
System.out.println("first name : " + emp.getFirstName());
System.out.println("last name : " + emp.getLastName());
return SUCCESS;
public Employee getModel() {
return emp;
public Employee getEmp() {
return emp;
public void setEmp(Employee emp) {
this.emp = emp;
package com.codeFactory.bean;public class Employee {private int id;
private String firstName, lastName;
public int getId() {
return id;
public void setId(int id) { = id;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
public String getLastName() {
return lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
package com.codeFactory.connection;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateConnection {Configuration cfg = new Configuration();
SessionFactory factory;
Session session1;
public Session getHbmConnection()
factory = cfg.buildSessionFactory();
session1 = factory.openSession();
return session1;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.codeFactory.bean.Employee" table="employee">
<id name="id">
<generator class="increment"></generator>
<property name="firstName"></property>
<property name="lastName"></property>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml" />




Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Rest API

What is HTTP? and HTTP Request Methods

HTTP Request and Response over Internet

JVM part 05 — ClassLoader

Unary Operator and loops in Java