Spring Tutorial – Connect Spring with Hibernate (ORM)

Spring with Hibernate

Spring Tutorial – Connect Spring with Hibernate (ORM) 

Overview

Spring ORM integration with Hibernate can be an important milestone to building a quality enterprise application. Spring is one of the most popular enterprise application development frameworks and Hibernate is one of the most popular database to object mapping frameworks. However, since there are multiple versions of both Spring and Hibernate, you need to note that there might be compatibility issues between both frameworks depending on several factors. For example, incompatibility issues could be the result of Hibernate classes moving between classes. There is no definitive guide on matching compatible versions, compatibility depends on several known and unknown factors. You need to find compatible versions. Compatibility issues should not discourage you from undertaking what is a rewarding initiative. Spring and Hibernate integration needs to follow a certain process or a series of steps to be successful. This article describes the standard steps and principles of integrating Spring ORM with Hibernate and provides an example.

Must Read – Steps to work with Spring Integration








Integration principles

You need to keep in mind the following broad principles.

  • You need to first find out compatible versions of Spring and Hibernate. Multiple versions are available but due to various reasons, not all versions of both frameworks may be compatible. To start, the versions need to be compatible.
  • Identify the dependencies carefully and specify ways your project is going to fulfil those dependencies. Also identify possible resolutions in cases the dependencies face an issue or a problem. Note that dependency-related issues are real and expected.
  • While every component in your project structure is important in its own right, you need to be especially careful in configuring the DAO classes because these classes will be constituting the interface to the database and persistent data. You can say the classes constitute the core of the integration project.

Integration steps

The objective is to provide the broad steps to integrate Spring ORM and Hibernate.

Configure the database

Identify the right database and set up the tables with the help of scripts.

Configure the project structure

The project should comprise everything you need to integrate Spring ORM and Hibernate. It will define how the various elements such as XML and SQL files. The image below shows an imaginary project structure which will be used for the integration.

State the dependencies

You need to state or define the dependencies on a project management tool such as Maven. The main dependencies are going to be the following:

  • Depending on the version of Spring you are using, declare the spring-tx and spring-context for the core Spring functionalities.
  • Spring ORM dependency to get Spring ORM support. This is one of the most important dependencies for integrating Spring and Hibernate.
  • The Hibernate framework is dependent on the hibernate-entitymanager and hibernate-core.
  • For database connection, state the dependency on the mysql-connector-java for MySQL driver.

Deciding between Model Class and Entity Bean

The decision will depend on your project considerations and requirements. Just to give a perspective to help your decision, a model object is a type of object that contains application data, provides access to the data and helps manipulate the data. A model class comprises one or more model objects. An entity bean, on the other hand, is a remote object that manages persistent data and performs manipulation on the data. It uses many dependent Java objects and is identified by a Primary Key.

Must Read – Learn Spring AOP in 30 minutes








Configuring Data Access Object (DAO) classes

You will need to decide the number of DAO classes you will have in the project, their role in the project, the methods you will add to the classes and their interaction with other entities that are attempting to access the database or persistent data through the DAO classes.

Configuring Spring Beans for Hibernate Integration

Now you are approaching the main objective of the project – integration between Spring and Hibernate. To configure, you need to provide database connection details to Hibernate. You can do it by providing all details in hibernate Properties or by creating a data source and passing it to Hibernate. Depending on how Spring and Hibernate are mapped, Spring ORM provides two classes:

  • springframework.orm.hibernate3.LocalSessionFactoryBean for XML-based mapping.
  • springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean for annotations-based mapping.

Must Read – Steps to integrate Spring MVC and AngularJS

Implementation

Assuming you have performed the above steps, it is now time to test run your project and find out how it does. Create  your own components and test the output.

Must Read – Spring with Testing frameworks – How does it work?








Conclusion

You need to note that the above steps are broad steps and in real-life integration scenarios, there can be variances considering the scenarios. Still, the broad principles and steps will remain the same. For example, you can choose XML-based or annotation-based mapping between the Spring and Hibernate or you can use one of the many ways available to connect the database with Hibernate. However, you need to note that the variances in project implementation can be mostly related to the differences arising because of the differences in implementation in case of different versions of Spring and Hibernate.

Please follow and like us:
0
============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Facebook
Facebook
YOUTUBE
LinkedIn