Learn How to Connect Spring ORM with Hibernate

1
2338
Connect Spring ORM

Connect Spring ORM

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.

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
This 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.

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 datasource and passing it to Hibernate. Depending on how Spring and Hibernate are mapped, Spring ORM provides two classes:
• org.springframework.orm.hibernate3.LocalSessionFactoryBean for XML-based mapping.
• org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean for annotations-based mapping.

Implementation
Let’s take an example to show the details. We will have the following components to make the application ready.
First one is an entity class; here it is Student entity containing student attributes. The class is described below

Listing 1: – Student entity class

Now the next class is the Student DAO interface containing two methods for adding students and displaying them whenever is requested.

Listing 2: – Student DAO class

Following is the DAO implementation class implementing the methods defined in the DAO interface.

Listing 3: – DAO implementation class

This is the manager interface. It takes the request from the controller and does the routing to the DAO implementation class.

Listing 4: – Student manager class

This is the manager implementation of the above methods.

Listing 5: – Student manager implementation class

Now, this is the controller class to control all the requests.

Listing 6: – Student controller class

This is the hibernate configuration file, to do the mapping

Listing 7: – Hibernate configuration class

Now, if the application is run, then it will have the capabilities to add students and then display the list wherever requested from any web application.

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.

1 COMMENT

  1. Hi,
    It was a nice tutorial until the end of it which you did not put your spring configuration file.
    please put also your spring configuration file.

    Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here