Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Hibernate Struts Integration

In this section, you will learn Hibernate Struts Integration.


How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

Hibernate Struts Integration

In this section, you will learn Hibernate Struts Integration.

Apache struts is the effective, efficient and extensible MVC based open source framework for developing Java web application. It supports the full development life cycle from developing, deploying to maintenance.

The WebWork and  Struts communities jointly work together to develop Struts2.


In the below example, we will create a login application using Struts and Hibernate.

The project hierarchy and the jar file used is given below :

The SQL query used to create table login is below :

CREATE TABLE `login` ( 
`loginid` varchar(30) NOT NULL, 
`password` varchar(30) default NULL, 
PRIMARY KEY (`loginid`) 



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="net.roseindia.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">none</property>

<mapping class="net.roseindia.Login"/>


label.username= UserName
label.password= Password
label.login= Login
error.login= UserName / Password Not Matching. Try again...

package net.roseindia;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
Configuration cfg = new Configuration();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
return cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
return sessionFactory;

package net.roseindia;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class Login implements Serializable{
private String loginid;

private String password;

public String getLoginid() {
return loginid;
public void setLoginid(String loginid) {
this.loginid = loginid;

public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;

package net.roseindia;

import org.hibernate.Query;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username;
private String password;

Session session=HibernateUtil.getSessionFactory().openSession();

public String execute() {
Query forlogin = session.createQuery("from Login as l where l.loginid = '"+this.username+"' and l.password='"+this.password+"'");
if (forlogin.list().size()==1) {
return "success";
} else {
return "error";
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title> World Struts Tutorial</title>

<h2>Struts 2 Login Example</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<h3>HELLO !! <font color=blue><s:property value="username" /> </font>!</h3>
<h2><font color=red>Welcome To RoseIndia</font></h2>


After execution, you will get the following page on your web browser :

The username is deepak and password is deepak . When you login correctly, you will get the following page :


If login is incorrect, you will get the following page :


Download  Source Code


Share on Google+Share on Google+

Hibernate Struts Integration

Posted on: March 27, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Struts Integration  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image