<p>In this Spring Hibernate Integration with Example tutorial we will see that how to configure the Hibernate with Spring framework with using one example.</p>
<h2>Spring Hibernate Integration</h2>
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;"><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/">Hibernate</a></b> is a powerful technology for persisting data in any kind of Application. <b>Spring</b>, on the other hand is a <b><a href="https://dineshonjava.com/dependency-injection-in-spring/">dependency injection</a></b> framework that supports <b><a href="https://dineshonjava.com/spring-ioc-container/">IOC</a></b>. The beauty of Spring is that it can integrates well with most of the prevailing popular technologies.</p>
<div class="separator" style="clear: both; text-align: center;"><img title="Spring Hibernate Integration with Example" src="https://dineshonjava.com/wp-content/uploads/2012/12/Spring-Hibernate-Integration-Example.png" border="0" /></div>
<p>If you are new to <b><i>Spring and Hibernate</i></b> frameworks, please read the introduction articles on <b><i>Spring and Hibernate</i></b> before start reading this article. <b><a href="https://dineshonjava.com/">DineshonJava</a></b> has explained in <a href="https://dineshonjava.com/spring-tutorial/">Introduction to Spring Framework</a>. This article will help you to understand the fundamentals of the <b><i>Spring framework</i></b>. In another article <b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/">Introduction to Hibernate</a></b> published on 27/03/2012 by <b><a href="https://dineshonjava.com/">DineshonJava</a></b> explains what is <b><i>ORM framework</i></b> and how to start writing the simple <b><i>hibernate</i></b> application.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div id="ads-id" align="center"></div>
<h2><b>Spring and Hibernate:</b></h2>
<p>As a pre-requisite, let us understand the need for such integration before we actually get into the integration between these two technologies. It is well known that <b>Hibernate</b> is a powerful <i><b>ORM tool</b></i> that lies between Application and Database. It enables Application to access data from any database in a platform-independent manner. There is no need for the Application to depend on the low-level <b>JDBC</b> details like managing connection, dealing with statements and result sets. All the necessary details for accessing a particular data source is easily configurable in Xml files. Another good thing is that Hibernate can be coupled well with both J2SE and J2EE Applications.</p>
<p>One of the problem with using <b>Hibernate</b> is that the client Application that accesses the database using <i><b>Hibernate Framework</b></i> has to depend on the <b>Hibernate</b> APIs like Configuration, SessionFactory and Session. These objects will continue to get scattered across the code throughout the Application. Moreover, the Application code has to manually maintain and manage these objects. In the case of <b>Spring</b>, the business objects can be highly configurable with the help of <i><b>IOC Container</b></i>. In simple words, the state of an object can be externalized from the Application code. It means that now it is possible to use the Hibernate objects as <i><b>Spring Beans</b></i> and they can enjoy all the facilities that Spring provides.</p>
<h2><b>Integration Spring and Hibernate</b></h2>
<p>We can simply integrate Hibernate configuration with the Spring than struts 2 , In hibernate framework file <i><b>hibernate.cfg.xml</b></i> using for the hibernate configuration with the application but in case of Spring there are no need to use this file we can simply configure with in the <i><b>Spring.xml</b></i> or <i><b>applicationContext.xml</b></i> configuration file. The following sections cover the various steps involved in the Spring-Hiberante integration along with a detailed explanation.</p>
<h3><b>Using Hibernate Mapping file with Spring</b></h3>
</div>
<ol style="text-align: left;">
<li>Creating Database and Table(<i><b>Employee Table</b></i>)</li>
<li>Creating Employee Class(<i><b>Employee.java</b></i>)</li>
<li>Creating the Hibernate Mapping file for Employee table(<i><b>Employee.hbm.xml</b></i>)</li>
<li>Creating the Spring Configuration File (<i><b>spring.xml</b></i>)</li>
<li>Creating the DAO class (<i><b>EmployeeDao.java</b></i>)</li>
<li>Creating Application Class for using this</li>
</ol>
<h3><b>We can also use the Annotation for using this example with following steps.</b></h3>
<ol style="text-align: left;">
<li>Creating Database and Table(<i><b>Employee Table</b></i>)</li>
<li>Creating Employee Class with using <b>@Entity</b> annotation(<i><b>Employee.java</b></i>)</li>
<li>Creating the Spring Configuration File (<i><b>spring.xml</b></i>) but creating bean with using annotation</li>
<li>Creating the DAO class (<i><b>EmployeeDao.java</b></i>)</li>
<li>Creating Application Class for using this</li>
</ol>
<p>Now first we see that example using annotation.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/12/hibernatespring.jpg" border="0" /></div>
<p><b>Step 1. Creating Database and Table(Employee Table)</b></p>
<pre class="highlight">CREATE TABLE Employee( 
 EMPID INT NOT NULL AUTO_INCREMENT, 
 NAME VARCHAR(20) NOT NULL, 
 AGE INT NOT NULL, 
 SALARY BIGINT NOT NULL, 
 PRIMARY KEY (ID) 
); 
</pre>
<p><b><br />
</b> <b>Step 2: Creating Employee Class</b><br />
<b><br />
</b> <b>Employee.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.domain; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Entity 
@Table(name="Employee") 
public class Employee { 
 @Id 
 @Column(name="EMPID") 
 @GeneratedValue(strategy=GenerationType.AUTO) 
 private int empid; 
 
 @Column(name="NAME") 
 private String name; 
 
 @Column(name="AGE") 
 private int age; 
 
 @Column(name="SALARY") 
 private long salary; 
 /** 
 * @return the empid 
 */ 
 public int getEmpid() { 
 return empid; 
 } 
 /** 
 * @param empid the empid to set 
 */ 
 public void setEmpid(int empid) { 
 this.empid = empid; 
 } 
 /** 
 * @return the name 
 */ 
 public String getName() { 
 return name; 
 } 
 /** 
 * @param name the name to set 
 */ 
 public void setName(String name) { 
 this.name = name; 
 } 
 /** 
 * @return the age 
 */ 
 public int getAge() { 
 return age; 
 } 
 /** 
 * @param age the age to set 
 */ 
 public void setAge(int age) { 
 this.age = age; 
 } 
 /** 
 * @return the salary 
 */ 
 public long getSalary() { 
 return salary; 
 } 
 /** 
 * @param salary the salary to set 
 */ 
 public void setSalary(long salary) { 
 this.salary = salary; 
 } 
 
 public String toString(){ 
 return "EMPLOYEE{empid- "+this.empid+" name- "+this.name+ 
 " age- "+this.age+" salary- "+this.salary+"}"; 
 } 
} 
</pre>
<p><b><br />
</b> <b>Step 3: Creating the Spring Configuration File </b><br />
<b>(spring.xml)</b></p>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:> 
<b> 
</b> <b>Step 4: Creating the DAO class </b> 
<b>(EmployeeDao.java)</b> 
<pre class="highlight">package com.dineshonjava.sdnext.dao; 
 
import java.util.List; 
 
import com.dineshonjava.sdnext.domain.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public interface EmployeeDao { 
 /** 
 * This is the method to be used to create 
 * a record in the Employee table. 
 */ 
 void createEmployee(Employee employee); 
 /** 
 * This is the method to be used to list down 
 * a record from the Employee table corresponding 
 * to a passed Employee id. 
 */ 
 Employee getEmployee(Integer empid); 
 /** 
 * This is the method to be used to list down 
 * all the records from the Employee table. 
 */ 
 List listEmployees(); 
 /** 
 * This is the method to be used to delete 
 * a record from the Employee table corresponding 
 * to a passed Employee id. 
 */ 
 void delete(Employee employee); 
 /** 
 * This is the method to be used to update 
 * a record into the Employee table. 
 */ 
 void update(Employee employee); 
} 
</pre>
<p><b><br />
</b> <b>SuperHibernateDaoSupport.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.dao.util; 
 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public abstract class SuperHibernateDaoSupport extends HibernateDaoSupport { 
 
} 
</pre>
<p><b><br />
</b> <b>EmployeeDaoImpl.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.dao.impl; 
 
import java.util.List; 
 
import org.springframework.stereotype.Repository; 
 
import com.dineshonjava.sdnext.dao.EmployeeDao; 
import com.dineshonjava.sdnext.dao.util.SuperHibernateDaoSupport; 
import com.dineshonjava.sdnext.domain.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Repository 
public class EmployeeDaoImpl extends SuperHibernateDaoSupport implements EmployeeDao { 
 
 @Override 
 public void createEmployee(Employee employee) { 
 getHibernateTemplate().saveOrUpdate(employee); 
 } 
 
 @Override 
 public Employee getEmployee(Integer empid) { 
 return (Employee) getHibernateTemplate().get(Employee.class, empid); 
 } 
 
 @Override 
 public List listEmployees() { 
 return getHibernateTemplate().find("FROM EMPLOYEE"); 
 } 
 
 @Override 
 public void delete(Employee employee) { 
 getHibernateTemplate().delete(employee); 
 } 
 
 @Override 
 public void update(Employee employee) { 
 getHibernateTemplate().saveOrUpdate(employee); 
 } 
} 
</pre>
<p><b>Step 5: Creating Application Class for using this</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.main; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
import com.dineshonjava.sdnext.dao.EmployeeDao; 
import com.dineshonjava.sdnext.domain.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmpMainApp { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
EmployeeDao empDao = (EmployeeDao) context.getBean("employeeDaoImpl"); 
 
 Employee employee = new Employee(); 
 employee.setName("Dinesh"); 
 employee.setAge(25); 
 employee.setSalary(50000l); 
 System.out.println("------Records Creation--------" ); 
 empDao.createEmployee(employee); 
 System.out.println("------Listing Multiple Records--------" ); 
 List employees = empDao.listEmployees(); 
 for (Employee emp: employees) { 
 System.out.print(emp); 
 } 
 System.out.println("------find one Records--------" ); 
 Employee employee = empDao.getEmployee(3); 
 System.out.print(employee); 
 System.out.println("------Delete one Records--------" ); 
 empDao.delete(employee); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b>Output:</b><br />
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).<br />
log4j:WARN Please initialize the log4j system properly.<br />
<b>------Records Creation--------</b><br />
Hibernate: insert into Employee (AGE, NAME, SALARY) values (?, ?, ?)<br />
<b>------Listing Multiple Records--------</b><br />
Hibernate: select employee0_.EMPID as EMPID0_, employee0_.AGE as AGE0_, employee0_.NAME as NAME0_, employee0_.SALARY as SALARY0_ from Employee employee0_<br />
EMPLOYEE{empid- 1 name- Dinesh age- 25 salary- 50000}EMPLOYEE{empid- 2 name- Anamika age- 20 salary- 30000}EMPLOYEE{empid- 3 name- Nimmo age- 24 salary- 30020}EMPLOYEE{empid- 4 name- Adesh age- 24 salary- 30011}EMPLOYEE{empid- 5 name- Vinesh age- 22 salary- 20011}EMPLOYEE{empid- 6 name- Rajesh age- 25 salary- 50000}EMPLOYEE{empid- 7 name- DAV age- 21 salary- 50000}<br />
<b>------find one Records--------</b><br />
Hibernate: select employee0_.EMPID as EMPID0_0_, employee0_.AGE as AGE0_0_, employee0_.NAME as NAME0_0_, employee0_.SALARY as SALARY0_0_ from Employee employee0_ where employee0_.EMPID=?<br />
EMPLOYEE{empid- 3 name- Nimmo age- 24 salary- 30020}<br />
<b>------Delete one Records--------</b><br />
Hibernate: delete from Employee where EMPID=?</div>
<p><b>Source Code from Git Link</b></p>
<p><a href="https://github.com/DOJ-SoftwareConsultant/SpringHibernateDemo.git">https://github.com/DOJ-SoftwareConsultant/SpringHibernateDemo.git</a></p>
<div style="text-align: center;"><b><;<;</b><b><b><a href="https://dineshonjava.com/transaction-management-in-spring/">Transaction Management</a></b></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| </b><b><b></b><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/">Spring Web MVC Framework</a></b></b><b>>;>;</b></div>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/declarative-transaction-management/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-aop-transaction-management-in/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '626'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…