Tuesday, December 4, 2012

Hibernate Struts Integration using Eclipse,Oracle and Hibernate Annotations


In this Tutorial we are creating database table and inserting data into that table using Jsp page, Hibernate and Struts Framework Integration.
Project View After Completion

Hibernate and Struts2 Setting
right click over project on Project Explorer and goto properties->Java Build Path->Libraries
Add Required Hibernate, struts jars, JRE system Library, Apache Tomcat server by click on Add Library and Add External Jar ojdbc14.jar by click on Add External Jars.
For Details Read How to set Struts2 Environment  and How to set Hibernate Environment

Properties->Web Deployment Assembly->Add->Java Build Path Entries->next
Select HibernateJars and StrutsJars and click finish
EmployeeEntry.jsp
This is page for user's input and here we using struts tags and on submit action is hello.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="s" uri="/struts-tags"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Insert title here</title>  
 </head>  
 <body>  
 <s:form action="hello">  
 <s:textfield name="emp_name" label="Employee Name:"/>  
 <s:textfield name="emp_sal" label="Employee Salary:"/>  
 <br/>  
 <s:token/>  
 <s:submit value="Insert" />  
 </s:form>  
 </body>  
 </html>  

EmpSuccess.jsp
This is a success page that displays success message on successful data entry into database table.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Success</title>  
 </head>  
 <body>  
 <h1> Data Entered Successfully</h1>  
 </body>  
 </html>  

EmployeeData.java
This is a POJO class and it is working as Persistent class for Hibernate and action class for Struts and the same java class we are using to store object of EmployeeData class using Hibernate Framework.
we are also using Annotations such as @Entity,@table,@Id etc.
If we don't use Hibernate Annotations in our project then we have to create mapping file(.hbm file)
 package org.webideaworld.integrate;  
 import javax.persistence.Entity;  
 import javax.persistence.GeneratedValue;  
 import javax.persistence.Id;  
 import javax.persistence.Table;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import org.hibernate.cfg.Configuration;  
 import com.opensymphony.xwork2.ActionSupport;  
 @Entity  
 @Table(name="EmployeeData")  
 public class EmployeeData extends ActionSupport{  
      @Id  
      @GeneratedValue  
      private int id;  
      private String emp_name;  
      private int emp_sal;  
      public int getId() {  
           return id;  
      }  
      public void setId(int id) {  
           this.id = id;  
      }  
      public String getEmp_name() {  
           return emp_name;  
      }  
      public void setEmp_name(String emp_name) {  
           this.emp_name = emp_name;  
      }  
      public int getEmp_sal() {  
           return emp_sal;  
      }  
      public void setEmp_sal(int emp_sal) {  
           this.emp_sal = emp_sal;  
      }  
      public String execute() throws Exception{  
           @SuppressWarnings("deprecation")  
           SessionFactory factory= new Configuration().configure().buildSessionFactory();  
           Session session = factory.openSession();  
           EmployeeData ed=new EmployeeData();  
           //sp.setId(01);  
           ed.setEmp_name(getEmp_name());  
           ed.setEmp_sal(getEmp_sal());  
           Transaction tx = session.beginTransaction();  
           int i=(Integer)session.save(ed);  
           tx.commit();  
           session.close();  
           factory.close();  
           if(i>0)  
           return SUCCESS;  
           else  
           return ERROR;  
      }  
 }  

hibernate.cfg.xml
This is a file that contains information about Oracle database(or any other database you want to use) Driver and connection information and mapping class information.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!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">oracle.jdbc.OracleDriver</property>  
   <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
   <property name="hibernate.connection.username">hr</property>  
   <property name="hibernate.connection.password">hr</property>  
   <property name="hibernate.connection.pool_size">10</property>  
   <property name="show_sql">true</property>  
   <property name="dialect">org.hibernate.dialect.OracleDialect</property>  
   <property name="hibernate.hbm2ddl.auto">update</property>  
   <mapping class="org.webideaworld.integrate.EmployeeData" />  
 </session-factory>  
 </hibernate-configuration>  

struts.xml
This file contains information about which action class to be invoked. here we are invoking EmployeeData action class using action name hello present in EmployeeEntry.jsp's page form tag on submit button click.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
 <struts>  
 <constant name="struts.devMode" value="true" />  
 <package name="helloworld" extends="struts-default">  
  <action name="hello" class="org.webideaworld.integrate.EmployeeData" method="execute">  
 <result name="success">/EmpSuccess.jsp</result>  
 <result name="error">/EmployeeEntry.jsp</result>  
 <result name="input">/EmployeeEntry.jsp</result>  
    </action>  
   </package>  
 </struts>       

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>HibernateStrutsIntegrate</display-name>  
  <welcome-file-list>  
    <welcome-file>EmployeeEntry.jsp</welcome-file>  
   </welcome-file-list>  
   <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

No comments:

Popular Posts