Integration of Hibernate with Spring MVC and Maven

Integration of Hibernate with Spring MVC and Maven is one of the very common and popular topics among the java developers who are either new to Spring or Hibernate or are currently working on an enterprise application involving these technologies. I hope we are all are aware that the Spring is already one of the leading enterprise application frameworks today, and Hibernate on the other hand is one of the most popular ORM tools.

ORM is the Object Relational Mapping of tables to the entity classes, rows to the class instances and columns of the table to attributes of the instances.

To start with the integration part, make sure that we have the database created. We have the MySQL database created as sample_db.

The first step is to create a new Maven project and quickly setup web.xml with proper dispatcherServlet configuration –

Create a new context file as <servlet name>-servlet.xml, which has to be mvc-dispatcher-servlet.xml with the following content –

Note that we have modularized our context file a bit, and imported another context file, hibernateContext.xml, for Hibernate specific configuration. Also the component-scanning is applied only to com.jcombat.controller package, so Spring can auto-detect the application controller.


There are other two beans we have configured in the context, userDaoImpl and userServiceImpl. Check the highlighted lines in the above code snippet.

Useful Reference Links:

Moving ahead, hibernateContext.xml should look like –

dataSource bean contains the database connection details. This dataSource bean reference is in turn used to setup up the sessionFactory bean of type LocalSessionFactoryBean. This sessionFactory bean contains other details like the mappingResources (any hbm files) and hibernateProperties.

Spring ORM provides us with two classes – LocalSessionFactoryBean if we need to go with XML based mapping and AnnotationSessionFactoryBean for the annotations based mapping.

Let’s now make sure we have the needed dependencies added to the pom.xml file as –

Now, let’s create User.java POJO class as –

We map this POJO in Hibernate within User.hbm.xml file as –

Its time to create the DAO layer. Let’s first create the interface UserDAO.java, which should be like –

Now create the DAO layer implementation class UserDAOImpl.java which implements the above UserDAO interface, and should be like –

Note that the sessionFactory in the above UserDAOImpl.java class is injected through Spring Dependency Injection based on the configuration we have done in mvc-dispatcher-servlet.xml.

Go on with creating the service layer, which will have UserService.java as the service interface, while UserServiceImpl.java as it’s implementation class. UserServiceImpl.java should look like –

Note that the userDAOImpl in the above UserServiceImpl.java class is also injected through Spring Dependency Injection based on the configuration we have done in mvc-dispatcher-servlet.xml.

Finally, we need to create the controller class. Let’s name it as DemoController.java

The final step is to create the view, home.jsp, that would be rendering the data contained by the model.

Now run the application on the server and we see the following getting displayed on the browser screen –

Integration of Hibernate with Spring

PERSISTENCE.XML VS HIBERNATE.CFG.XML VS HIBERNATECONTEXT.XML

We might have come across places where we use persistence.xml, while at some other places we use hibernate.cfg.xml or hibernateContext.xml. So the question is, where do we use what? Below is what you need to know –

If we are using Hibernate’s proprietary API, we will need the hibernate.cfg.xml. But if we are using JPA, we need to explicitly specify the provider whose EntityManager implementation will be used by JPA. In this case, we need to specify the provider in persistence.xml. We generally don’t need to use both. hibernate.cfg.xml is for Hibernate, while persistence.xml is for JPA.

Using hibernate.cfg.xml or a separate hibernateContext.xml imported within applicationContext, as we have done in this article, is almost one and the same thing. If we are using Hibernate’s proprietary API, the configuration is read from classpath:hibernate.cfg.xml by default. However Spring’s LocalSessionFactoryBean allows us to setup hibernate configuration inside applicationContext instead of hibernate.cfg.xml. If both hibernate.cfg.xml as well as LocalSessionFactoryBean configuration in applicationContext are present, the hibernate property configuration for LocalSessionFactoryBean will take priority over hibernate.cfg.xml.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.