Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Data Access Using Spring Framework: HibernateTemplate

Save for later
  • 4 min read
  • 12 Oct 2011

article-image

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.

HibernateTemplate


Spring Framework provides different approaches to integrate with Hibernate. However, the commonly used approach is using HibernateTemplate. There are two main reasons. They are:

  1. Hiding away the session and transaction management details
  2. Providing template based approach


The former takes care of infrastructure management while the later provides a consistent way to implement data access layer.

  1. Hiding away the session and transaction management detailsHibernateTemplate class hides away the complexity of managing session and transaction while accessing data using Hibernate. One only has to instantiate the HibernateTemplate by passing an instance of SessionFactory. From thereon, the session and transaction related details will be taken care of by Spring Framework. This helps by eliminating the need for infrastructure code that may become cluttered as the complexity of the application increases.
  2. Providing template based approachHibernateTemplate, like JdbcTemplate, provides a template based approach to data access. Due to this, one need to follow the approach dictated by the template and thus consistency is maintained in the coding when compared to the traditional way of data access. When you are using HibernateTemplate, you will be working with callbacks. Callbacks are the only mechanism in templating approach to instruct the template to execute a particular task. The advantage of having a callback is that there only one entry point into the data access layer. And this entry point is defined by the template, in this case HibernateTemplate, thus providing a consistent approach. In nutshell, the template based approach provides consistency and makes the code maintainable.


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:

  1. Convenience/Helper Methods
  2. Template methods
  3. Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at ₹800/month. Cancel anytime


The former relates to the data retrieval and manipulation API of Hibernate and the latter relates to the session and transaction management API.

  1. Convenience/Helper MethodsHibernate has API that simplifies CRUD (Create, Retrieve, Update, Delete) operations. The helper methods of HibernateTemplate provide a wrapper around these so that the Template methods can take care of session and transaction management. As a developer, you will directly make calls to the helper methods without explicitly opening and closing the session. The helper methods include find(), saveOrUpdate(), delete() etc. You can get a complete list from the following link:
    http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/orm/hibernate3/HibernateTemplate.html
  2. Template methodsTemplate or callback methods are central to HibernateTemplate as they streamline the way operations on data is performed. There are four main template methods. They are:
    • Execute
    • ExecuteWithNativeSession
    • ExecuteWithNewSession
    • ExecuteFind


    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.