Spring MVC + Hibernate XML Based Configuration Example

In this tutorial, we will learn how to create a Spring MVC project ( without Spring Boot) and integrate Spring MVC with Hibernate ORM framework using XML-based configuration.

For Java-based configuration, check out Spring MVC 5 + Hibernate 5 + JSP + MySQL CRUD Tutorial.

To integrate Hibernate with the Spring MVC application, you can use the LocalSessionFactoryBean class, which setup a shared SessionFactory object within a Spring application context. This SessionFactory object can be passed to DAO classes via dependencies injection.

Short note on how Spring supports Hibernate integration:

Basically, in order to support Hibernate integration, Spring provides two key beans available in the org.springframework.orm.hibernate5 package:

LocalSessionFactoryBean: creates a Hibernate’s SessionFactory which is injected into Hibernate-based DAO classes.

HibernateTransactionManager: provides transaction support code for a SessionFactory. Programmers can use @Transactional annotation in DAO methods to avoid writing boiler-plate transaction code explicitly.

Tools and technologies used

Let me list out development steps so that it will be easy to develop and understand the Spring MVC application step by step.

Development Steps

  1. Create Maven Web Application
  2. Add Dependencies - pom.xml File
  3. Project Structure
  4. web.xml - Register a DispatcherServlet using XML-based Spring configuration
  5. spring-mvc-crud-demo-servlet.xml - Spring and Hibernate Integration using XML-based Spring configuration
  6. JPA Entity - Customer.java
  7. Spring MVC Controller Class - CustomerController.java
  8. Service Layer - CustomerService.java and CustomerServiceImpl.java
  9. DAO Layer - CustomerDAO.java and CustomerDAOImpl.java 11 JSP Views - customer-form.jsp and list-customers.jsp
  10. Serve Static Resources - CSS and JS
  11. JSP Views - customer-form.jsp and list-customers.jsp
  12. Build and Run an application
  13. Demo

1. Create a Maven Web Application

Let's create a Maven-based web application either using a command line or from Eclipse IDE.