Even though ORM framework such as Hibernate hides away most of the complexities related to heterogeneous database systems, the framework has their own infrastructure management requirements including session and transaction management. Such requirements can be managed well using another framework that uses dependency injection to provide and manage the requirements of Hibernate. Spring Framework is one of such most commonly used frameworks. It provides first class integration with Hibernate through its HibernateTemplate. It is analogous to the JdbcTemplate that is used to integrate with JDBC API. In this discussion, the focus will be on using HibernateTemplate to integrate Hibernate with Spring Framework. The first section will introduce you to the HibernateTemplate and details the requirements for using it. In the second section, I will detail the steps for using Hibernate with Spring Framework. In the last section, a real world application will be developed using the steps detailed in the last section. This is the outline for this discussion.
Spring Framework provides different approaches to integrate with Hibernate. However, the commonly used approach is using HibernateTemplate. There are two main reasons. They are:
The former takes care of infrastructure management while the later provides a consistent way to implement data access layer.
Now that we have discussed the advantages/reasons to use HibernateTemplate, let us proceed towards the functionalities provided by it. The API provided by it can be categorized into the following:
The former relates to the data retrieval and manipulation API of Hibernate and the latter relates to the session and transaction management API.
Of these, Execute is most commonly used one. To use any of these methods, one just needs to create an instance of HibernateTemplate by passing an instance of SessionFactory to the constructor of HibernateTemplate. All the forms of Execute method take instance of HibernateCallback class and execute the data access logic contained in the instance of the class extending HibernateCallback.
That brings us to the end of this section. However, before moving onto the steps for using HibernateTemplate, it is important to keep in mind the versions of Spring Framework and Hibernate that you will require in the steps to be described in next section. The version of Spring Framework is 2.x and that of Hibernate is 3.x. The important point to remember is that 2.x versions of Spring Framework support Hibernate 3.x versions only. So the libraries that you require for Hibernate 3.x will be required to make the examples detailed in this discussion to work. Same goes for Spring 2.x dependencies.
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…