Nearby lessons

6 of 19

Hibernate - Integration with Struts

📌 What You Will Learn
  • Understand Hibernate - Integration with Struts
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Integration with Struts step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Integration with Struts

  • <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.c om/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" id="WebApp_ID" version="2.5">

  • <display-name>loginapp</display-name>
  • <welcome-file-list>
  • <welcome-file>loginform.html</welcome-file>
  • </welcome-file-list>
  • <servlet>
  • <servlet-name>actionServlet</servlet-name>
  • <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  • <load-on-startup>1</load-on-startup>
  • </servlet>
  • <servlet-mapping>
  • <servlet-name>actionServlet</servlet-name>
  • <url-pattern>*.do</url-pattern>
  • </servlet-mapping>
  • </web-app>

Integration Of Hibernate with Struts

  • Manual Approach:
  • By Customizing ActionServlet
  • By Using PlugIn Approach

Hibernate

  • Student.java
  • Student.hbm.xml
  • hibernate.cfg.xml
  • HibernateUtil.java
  • StudentService.java

Struts — registrationform.html

  • StudentRegistrationAction.java
  • StudentRegistrationActionForm.java
  • student_registration_form.html
  • success.html
  • failure.html
  • existed.html
  • struts-config.xml
  • web.xml

Example:

Example04
Loading editor…

Struts — success.html

Example05
Loading editor…

Struts — failure.html

Example06
Loading editor…

Struts — existed.html

Example07
Loading editor…

Struts — StudentActionForm.java

Example08
Loading editor…

Struts — StudentAction.java

Example09
Loading editor…

Struts — StudentService.java

Example10
Loading editor…

Struts — Student.java

Example11
Loading editor…

Struts — HibernateUtil.java

Example12
Loading editor…

Struts — Student.hbm.xml

Example13
Loading editor…

Struts — hibernate.cfg.xml

Example14
Loading editor…

Struts — struts-config.xml

Example15
Loading editor…

Struts — web.xml

Example16
Loading editor…

Struts

In the above application, Struts Framework will be loaded at the time of Server Startup, because, ActionServlet initialization is performed at the time of Server Startup due to <load-on-startup> configuration in ActionServlet configuration in web.xml file, but, Hibernate software is loaded after the Server startup and after getting first request from client, it may take lot of time to load Hibernate Software, it will increase response time and it will reduce application performance.

In the above context, to improve application performance we have to load Hibernate Software along with Struts framework loading, that is, at the time of Server startup and as part of ActionServlet initialization.

To achieve the above requirement we have to Customize ActionServlet initialization process.

Hibernate integration with Struts by Customizing ActionServlet

1) Prepare Our own Controller class by extending ActionServlet and override "init()"

method

Example19
Loading editor…

Configure User Defined Controller class in web.xml file

  • <web-app>
  • <welcome-file-list>
  • <welcome-file>registrationform.html</welcome-file>
  • </welcome-file-list>
  • <servlet>
  • <servlet-name>myController</servlet-name>
  • <servlet-class>com.durgasoft.controller.MyController</servlet-class>
  • <load-on-startup>1</load-on-startup>
  • </servlet>
  • <servlet-mapping>
  • <servlet-name>myController</servlet-name>
  • <url-pattern>*.do</url-pattern>
  • </servlet-mapping>
  • </web-app>

In Struts based web applications, it is not suggestible to customize predefined controller ActionServlet , because, ActionServlet is having no of responsibilities as part of its initialization, if we have not overridden init() method properly then the total ActionServlet may not be executed, it may not load Struts Framework.

Integration of Hibernate with Struts by using plugin Approach

a) Prepare Hibernate PlugIn class. b) Configure hibernate PlugIn class in struts configuration file.

a) Prepare Hibernate PlugIn class

To prepare Hibernate PlugIn class, declare an user defined class and implement org.apache.struts.action.PlugIn interface and provide implementat6ion for the following methods.

  • public void init(ActionServlet as, ModuleConfig config) throws ServletException
  • public void destroy()
Example22
Loading editor…

b) Configure Hibernate Plugin class in Struts configuration File

  • <struts-config>
  • <plug-in className="com.durgasoft.plugin.HibernatePlugIn"/>
  • </struts-config>

Internal Flow

When we start Server, Container will perform the following actions.

  • Container will deploye all web applications which are available in webapps folder or in deployments folder.
  • Container will recognize web.xml file and perform web.xml file loading and parsing.
  • Container will recognize <load-on-startup> configuration in ActionServlet configuration in

web.xml file then performs ActionServlet loading, instantiation and initialization.

  • As part of ActionServlet initialization, ActionServlet will recognize struts configuration file

then performs struts configuration file loading and parsing.

  • ActionServlet will recognize <plug-in> tag in struts configuration file and identify

HibernatePlugIn class .

  • ActionServlet will perform HibernatePlugIn class loading , instantiation and initialization.
  • As part of HibernatePlugIn class initialization,ActionServlet will load Hibernate software and

prepare Configuration and SessionFactory objects with HibernateUtil class at the time of Server startup. Is it possible to interact with more than one Database from a single Hibernate Application?

Ans — Employee.java

Yes, in single hibernate application we are able to interact with more than one Database, but, we must use the following conventions.

1) We must provide separate configuration file for each and every Database. 2) Use either single mapping file if we have same table name and same columns in both the

databases tables and use different mapping file for each and every database if we have difference in table names and column names. 3) In Client application.

  • Prepare Seperate Configuration object for each and every DB.
  • Prepare seperate SessionFactory object for each and every DB.
  • Prepare seperate Session object for each and every DB.
  • Prepare Seperate Transaction object for each and every DB as per the requirement.
  • Perform persistance operations on DB respective Session objects.

Example:

Example25
Loading editor…

Ans — Employee.hbm.xml

Example26
Loading editor…

Ans — hibernate_oracle.cfg.xml

Example27
Loading editor…

Ans — hibernate_mysql.cfg.xml

Example28
Loading editor…

Ans — ClientApp.java

Example29
Loading editor…

Ans

Is it possible to run hibernate applicatins with out using mapping file?

ANS

Yes, it is possible to run hibernate applications with out using mapping file, but, we have to use Annotations. To provide mapping details in Hibernate applications we have to use the following annotations provided by JPA in the form of "javax.persistence" package.

@Entity

It is a class level annotation, it can be used to declare a class as an Entity class.

  • @Table(name="--")

This annotation is a class level annotation, it can be used to configure table name for the entity class. Where "name" member is able to provide the respective table name.

@Id — hibernate.cfg.xml

This annotation is Field/Method[getXXX()] level annotation, it can be used to declare a property as ID property.

  • @Column(name="--")

This annotation is Field/Method[getXXX()] level annotation, it can be used to provide a database table column name inorder to provide mapping .

1) In Hibernate Applications, to use annotations we have to use the following steps. 2) Remove mapping file and provide annotations ion POJO class. 3) In Configuration File , provide <mapping> tag with "class" attribute inplace of "resource"

attribute."class" attribute will take fully qualified name of the respective POJO class. 4) In Client Application, Create Either AnnotationConfiguration object or Configuration object to

process Annotations. Note: AnnotationConfiguration is deprecated class, so it is better to use Configuration class object to process annotations.

Ex: Employee.java

  • @Entity
  • @Table(name="emp1")
  • public class Employee{
  • @Id
  • @Column(name="ENO")
  • private int eno;
  • @Column(name="ENAME")
  • private String ename;
  • @Column(name="ESAL")
  • private float esal;
  • @Column(name="EADDR")
  • private String eaddr;
  • setXXX() and getXXX()
Example33
Loading editor…

ClientApp.java — Employee.java

Same as previous applications.

Example:

Example34
Loading editor…

ClientApp.java — hibernate.cfg.xml

Example35
Loading editor…
📝 Key Takeaways
  • Key ideas of Hibernate - Integration with Struts explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.