Note that there are some explanatory texts on larger screens.

plurals
  1. POIntegrating struts2+spring+Hibernate
    primarykey
    data
    text
    <p>I was going through hibernate these days then I was trying to develop an applicattion to integrate struts2+Hibernate+Spring..I have two approaches to intialize the hibernate session factory..one is as we load the filter dispatcher for struts2 at that time we should load the hibernate sessionfactory for example as shown below..</p> <p>Web.xml...</p> <pre><code>&lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;filter&gt; &lt;filter-name&gt;controller&lt;/filter-name&gt; &lt;filter-class&gt;mypack.Struts2Dispatcher&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;controller&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt;&lt;/web-app&gt; </code></pre> <p>Struts2Dispatcher file..</p> <pre><code>import javax.servlet.*; import org.apache.struts2.dispatcher.FilterDispatcher; import org.hibernate.HibernateException; public class Struts2Dispatcher extends FilterDispatcher { @Override public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); try { HibernateUtil.createSessionFactory(); System.out.print("-------application initializing successfully-----"); } catch (HibernateException e) { throw new ServletException(e); } } } </code></pre> <p>and hibernate util ..</p> <pre><code>package mypack; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.Session; public class HibernateUtil { private static SessionFactory sessionFactory; public static void createSessionFactory() { sessionFactory = new Configuration().configure().buildSessionFactory(); } public static Session getSession() { return sessionFactory.openSession(); } } </code></pre> <p>The configuration file..</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;!-- Generated by MyEclipse Hibernate Tools. --&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="connection.username"&gt;system&lt;/property&gt; &lt;property name="connection.url"&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.Oracle9Dialect&lt;/property&gt; &lt;property name="connection.password"&gt;manager&lt;/property&gt; &lt;property name="connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;mapping resource="Employee.hbm.xml"/&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>the mapping file..</p> <pre><code>&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="mypack.Employee"&gt; &lt;id name="eid" column="emp_id" type="int"&gt; &lt;generator class="increment"/&gt;&lt;/id&gt; &lt;property name="name" column="emp_name"/&gt; &lt;property name="job"/&gt; &lt;property name="salary" type="int"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The application context.xml file</p> <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt; &lt;bean id="MyIOCObject" class="mypack.LoginAction"/&gt; &lt;/beans&gt; </code></pre> <p>the struts.xml file..</p> <pre><code>&lt;struts&gt; &lt;package name="pack" extends="struts-default"&gt; &lt;action name="login" class="MyIOCObject" method="insert"&gt; &lt;result name="success"&gt;/welcome.jsp&lt;/result&gt; &lt;result name="failure"&gt;/relogin.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt; </code></pre> <p>The login Action java class</p> <pre><code>package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; public class LoginAction { private String name,job; private int salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public String insert() { try { Session ses=HibernateUtil.getSession(); Employee emp=new Employee(name,job,salary); Transaction tx=ses.beginTransaction(); ses.save(emp); tx.commit(); ses.close(); return "success"; }catch(Exception e) { System.out.println(e); return "failure"; } } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } } </code></pre> <p>The main employee pojo</p> <pre><code>package mypack; public class Employee { private int eid,salary; private String name,job; public Employee() { super(); } public Employee(String name, String job,int salary) { super(); this.salary = salary; this.name = name; this.job = job; } public int getEid() { return eid; } public void setEid(int eid) { this.eid = eid; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } } </code></pre> <p>The main Test program..</p> <pre><code>package mypack; import java.util.Scanner; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class TestProgram { public static void main(String[] args) { try { Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sesfac=cfg.buildSessionFactory(); Session ses=sesfac.openSession(); System.out.println("Session created , fetching objects..."); Scanner in=new Scanner(System.in); System.out.println("Enter Employee id :- "); int id=in.nextInt(); Employee e=(Employee)ses.load(Employee.class,id); System.out.println("Following Object is fetched"); System.out.println(e.getEid()+"\t"+e.getName()+"\t"+e.getSalary()+"\t"+e.getJob()); ses.close(); }catch (Exception e) { System.out.println(e); } } } </code></pre> <p>and the other approach that I was discussing is on the link <a href="http://www.mkyong.com/struts2/struts-2-spring-hibernate-integration-example/" rel="nofollow">another approach to integagrate </a> Now please advise me which approach is best and is there any other approach better then these two..?</p> <p>Please post the updated code so That I can grasp the concept, Please advise..!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload