Table of contents
1.
Introduction
2.
Struts Framework
3.
Hibernate and Struts Integration
4.
Sample Files for Integration
5.
Frequently Asked Questions
5.1.
How does Hibernate work?
5.2.
What is the use of generator class in hibernate?
5.3.
Define persistent class.
5.4.
What is a Hibernate Configuration File?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Hibernate and Struts Integration

Author Manish Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninja!! While the hibernate framework provides a lot of tools for easy database integration of Java Apps. You can also integrate hibernate with the Struts framework without any hassle. It is super simple and makes architecture elegant and standard with the current development style. Let's jump right into the details of Hibernate and Struts Integration.
 

Hibernate & Struts logo

Struts Framework

It is an open-source web application framework for developing Java-based web projects. It is based on Java Servlet API and encourages developers to adopt the MVC development model. It was donated to Apache Foundation in May 2000. Hibernate and Struts Integration was made possible in subsequent releases.

Hibernate and Struts Integration

Hibernate and Struts Integration is quite easy. You need the jar files of Struts and Hibernate to perform the integration. No extra effort is required. 

Here are some common files that we will use for the Hibernate and Struts Integration.

  • Index.jsp
  • User.java
  • SaveDao.java
  • User.hbm.xml
  • Hibernate.cfg.xml
  • Struts.xml
  • Welcome.jsp
  • Web.xml
     

We will discuss the Hibernate and Struts integration through the example given below.

Sample Files for Integration

In this example, we will put the data into the database using hibernate and use Struts for developing the registration form. Let us go through the required files one by one for successful hibernate and struts integration:

 

Index. jsp: We use this file to generate the form using Struts tags and take the input. The action name for this form is ‘save’.

 

//CodingNinjas index file code
<%@ taglib uri="/struts-tags" prefix="S" %>    
<S:form action="save">  
<S:textfield label="Name" name="name" ></S:textfield>  
//action button
<S:submit value="save"></S:submit>   
</S:form>  
You can also try this code with Online Java Compiler
Run Code

 

User.java: It is a Plain Old Java Object (POJO) class. It works as the persistent class for hibernate and action class for struts. It calls the save method of SaveDao class and returns ‘saved’ as the string.
 

//CodingNinjas
//User class code 
package com.codingninjas;    
public class User {  
private int id;  
private String name;    
public String execute(){  
    SaveDao.saveUser(this);  
    return "saved";  
}    
}  
You can also try this code with Online Java Compiler
Run Code

 

SaveDao.java is a java class to save objects created using the User class with the hibernate framework.
 

//Coding Ninjas
//Class to save the user object using hibernate framework
package com.codingninjas;    
import org.hibernate.Session;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  
  
public class SaveDao {  
  
public static int saveUser(User u){            
Session s=new Configuration().  
configure("hibernate.cfg.xml").buildSessionFactory().openSession();            
Transaction trans=s.beginTransaction();  
int k=(Integer)s.save(u);  
trans.commit();  
s.close();            
return k;  
  }    
} 
You can also try this code with Online Java Compiler
Run Code


User.hbm.xml: It is a mapping file containing all the persistent class information.
 

<!-- Sample xml code for mapping : CodingNinjas -->
<?xml version='1.0' encoding='UTF-8'?>     
<hibernate-mapping>  
<class name="com.CodingNinjas.User" table="user321">  
<id name="id">  
<generator class="increment"></generator>  
</id>  
<property name="name"></property>  
</class>              
</hibernate-mapping>

 

Hibernate.cfg.xml: A file that contains configuration information about the mapping file and database. Here we are using hb2ddl.auto property.
 

<!--Hail CodingNinjas :)-->  
<!-- Configuration File -->
<?xml version='1.0' encoding='UTF-8'?>   
<hibernate-configuration>    
<session-factory>  
<property name="hbm2ddl.auto">update</property>  
<property name="dialect">org.hibernate.dialect.Oracle10Dialect</property>  
<property name="connection.url">jdbc:oracle:thin:@localhost:3000:xe</property>  
<property name="connection.username">Ninja</property>  
<property name="connection.password">CN</property>  
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
<mapping resource="user.hbm.xml"/>        
</session-factory>    
</hibernate-configuration>

 

struts.xml: It contains information about which class has to be invoked. The user class is the primary action class.
 

<!--Coding Ninjas-->
<!-- Struts xml file-->
<?xml version="1.0" encoding="UTF-8" ?>  
<struts>    
<package name="abc" extends="struts-default">  
<action name="save" class="com.CodingNinjas.User">  
<result name="saved">welcome.jsp</result>  
</action>  
</package>  
</struts> 

 

Welcome.jsp: It displays the welcome message on the screen along with the username.
 

<%@ taglib uri="/struts-tags" prefix="S" %>    
Welcome: <S:property value="name"/>

 

Web.xml: It contains information about the controller.
 

<!--Coding Ninjas-->
<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0"   
    xmlns="https://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="https://java.sun.com/xml/ns/javaee   
    https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>  
  </filter>  
  <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
</web-app>  

Frequently Asked Questions

How does Hibernate work?

Hibernate uses XML files to map Java classes to database tables, so you don't have to write code. Provides simple APIs for directly storing and retrieving Java objects from and to the database. If the database or table changes, you only need to change the XML file properties.

What is the use of generator class in hibernate?

A generator class is utilised when creating an ID as a primary key in the database. The persistent Class's objects have an ID as a unique identification. We can use any generator classes in our program according to our needs.

Define persistent class.

Persistent classes are those whose objects are stored in a database table.

What is a Hibernate Configuration File?

Hibernate Configuration Files are used to initialise SessionFactory and primarily include database-specific configurations. Dialect information so that Hibernate knows the database type and mapping file or class details are two crucial aspects of the Hibernate Configuration File.

Conclusion

In this article, we have extensively discussed the Hibernate and Struts integration.

After reading about Hibernate and Struts Integration, are you not feeling excited to read/explore more articles on the topic of hibernate? Don't worry; Coding Ninjas has you covered. If you want to check out articles related to hibernate, refer to these links, Hibernate SessionsHB web applicationHibernate AnnotationsHibernate Architecture etc.

Meme

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 
 

But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our Hibernate and Struts Integration blog if you found it helpful and engaging!

Happy Learning!

Ninja Image

 

Live masterclass