Integrating Hibernate and Spring

The data support framework of Spring provides extensive utilities to integrate Hibernate. It is common to find many production applications using Hibernate as an ORM framework in Spring applications. In fact, they go pretty well with each other in leveraging the productivity of the developer.

Watch Out

As a Java programmer, sooner or later, you will encounter these two architectures and any seasoned programmer will tell you that the worst part of integrating two frameworks can be a nightmare. The dependency and configuration issues can be irritating at times, especially due to version mismatch or any other configuration changes, etc. While integrating Hibernate and Spring there are a lot of configuration, stereotype annotation issues that needs to be taken care of. However, do not let that stop you; be assured that the end result is always sweet. Assuming one has some experience with Spring and Hibernate but has never combined them, let us integrate and go hands-on with the help of a sample application.

Dependency Workout

We can use any project management and comprehension tool for integrating the required dependencies in our project. This can be especially helpful while dealing with complex dependencies. Maven is a popular project management tool that provides a uniform build system. But we will be a little rigid here, and go the old-fashioned way of downloading the frameworks and required jars individually. Once downloaded, create a library of each of them and import them into the project. To be specific our project has following dependencies apart from Java SDK, Java EE7 (especially for using JPA), MySQL5 and Netbeans IDE.

1. Spring Framework (version 4.x): Spring is comprehensively featured framework, specifically for enterprise development. Creating EJB in J2EE used to be a cumbersome experience, however that is not the case now, especially from EE7. Spring eased this difficulty to a great extent with many other features. However, one thing for sure Spring framework provides almost every feature that the current trend of enterprise development needs and one may not have to look back for more.

2. Hibernate ORM Framework (version 4.x): Where iBatis retired to namesake MyBatis, Hibernate rules the ORM world in Java. This de-facto standard of object relational modeling influenced JPA specification when Entity Beans of EJB were fumbling for an inspiration. Hibernate is going to stay in the time line of oblivion for a many years to come and evolve significantly. This can be an important ORM sidekick of the Spring framework when used effectively.

3. Apache Commons DBCP (version 2.x) and Commons Pool (version 2.x): Jestingly, Apache is full of commons (sense). They invariably provide something or the other for almost every framework in the market. These two jars provide data source related implementation and pooling of database connection. However, Spring also provides an inherent data source related implementation, for example, DriverManagerDataSource for JDBC. But apart from many other third party implementations, DBCP along with Commons Pool are some of the popular connection and pooling related implementations used in the industry. Read DBCP overview and Commons Pool overview for more detail.

4. Apache Commons AOP Alliance (Version 1.0.0): See, common (sense) again. This is one of the dependencies for the Spring framework especially for AOP (Aspect Oriented Programming). It generally comes with the Spring library. But the case is that a little older version of the Spring framework provides the beta version of this jar. So depending on the version one is using one may or may not have to specifically download this jar and add on to the project. I found a lot of errors and warnings due to the beta version of this jar in my Spring library.

5. Apache Commons Logging (Version 1.x): And yet again, this jar is necessary only if the logging feature is used in the project and it’s a common practice to log information of various actions at application run time. As said, unnecessary but good to include one.

6. MySQL Connector/J (Version 5.x): This is MySQL JDBC driver and comes in a jar file. Since we will be using MySQL database at the back end, this driver is a must.

Let's Start

Our project will have two model classes – Contact and Address with one to many relation between them. One data access object interface – ContactDao and its implementation class ContactDaoImpl. The code given below is simple and self-explanatory for anyone with a little familiarity with these frameworks. Use the Java docs for additional information. The XML configuration file, spring.xml is the crux of integrating Spring and Hibernate.

Conclusion

The main hurdle for first time users would be writing the right configuration parameters in the XML file. Working individually with Spring and Hibernate is not much of a problem. Watch out for version mismatch between Hibernate and Spring and any other jars; consult their manual if any error crops up during compilation or runtime execution. Also watch out for <beans xmlns... in spring.xml declaration parameters. A mistake in the code is visible most of the time but a mistake in the configuration parameters is not always obvious. Happy coding!