Spring 5 and Hibernate 5 integration CRUD Example

By | June 24, 2021

In this post, We will learn about Spring 5 and Hibernate 5 integration CRUD Example using a Demo Project.

In hibernate framework, we usually provide all the database information in the hibernate.cfg.xml file.

But when we integrate the hibernate application with spring, we really don’t need to create the hibernate.cfg.xml file. We can configure all the information in the applicationContext.xml file.

pom.xml

We need to add Spring, Hibernate and MySQL dependencies in Maven pom.xml file.

Employee.java

Employee entity class to store employee data

EmployeeService.java

Employee Service Interface

EmployeeServiceImpl.java

Employee Service Implementation class

EmployeeDao.java

Employee DAO(Data Access Object) Interface

EmployeeDaoImpl.java

Employee DAO(Data Access Object) Interface implementation class to perform CRUD operations with MySQL database.

applicationContext.xml

In this config file, we are providing all the information of the database in the DriverManagerDataSource object. This object is used in the LocalSessionFactoryBean class object, containing some other information such as packagesToScan and hibernateProperties. The object of LocalSessionFactoryBean class is used to inject sessionFactory in EmployeeDaoImpl class using setter based dependency injection

ClientTest.java

This ClientTest class gets the bean from the applicationContext.xml file and calls the different methods of  EmployeeService to perform CRUD operation with MySQL database.

If you run ClientTest.java as Java Application then it will give the below output:

MySql Database Screen Shot After running Client Program:

That’s all about Spring 5 and Hibernate 5 integration CRUD Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.