Integrating Hibernate and Spring

Monday Jul 14th 2014 by Manoj Debnath

Learn how to be become more productive by integrating Hibernate in your Spring applications.

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.

Listing 1: spring.xml configuring Spring and Hibernate

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       xsi:schemaLocation="   ">
    <!--<context:annotation-config />-->
    <tx:annotation-driven />
    <context:component-scan base-package="" />
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
                  value="jdbc:mysql://localhost:3306/contact_db?zeroDateTimeBehavior=convertToNull" />
        <property name="username" value="user" />
        <property name="password" value="secret" />
    <bean id="sessionFactory"
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="" />   
        <property name="hibernateProperties">
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="">create-drop</prop>              
        <property name="annotatedClasses">
    <bean id="transactionManager"

Listing 2: model class for Contact

//...import  statements
@Table(name = "contact")
public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private long id;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;
    @Column(name = "BIRTH_DATE")
    private Date birthDate;
    @Column(name = "EMAIL")
    private String email;
    @Column(name = "PHONE_NUMBER")
    private String phoneNumber;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "contact", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Address> contactAddress = new HashSet<>();
    public Contact() {
    public Contact(String firstName, String lastName, Date birthDate, String email, String phoneNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate; = email;
        this.phoneNumber = phoneNumber;
    public void addAddress(Address address) {
    public void removeAddress(Address address) {
   //...getters and setters
    public String toString() {
        return firstName + " " + lastName + "\nBirth Date: " + birthDate + "\nEmail: " + email + " \nPhone Number: " + phoneNumber ;

Listing 3: model class for Address

//...import statements
@Table(name = "contact_address")
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private long id;
    @Column(name = "STREET")
    private String street;
    @Column(name = "CITY")
    private String city;
    @Column(name = "COUNTRY")
    private String country;
    @Column(name = "ZIP")
    private String zip;
    @JoinColumn(name = "CONTACT_ID")
    private Contact contact;
    public Address() {
    public Address(String street, String city, String country, String zip) {
        this.street = street; = city; = country; = zip;
//...getters and setters
    public String toString() {
        return  street + ", city=" + city + ", country=" + country + ", zip=" + zip;

Listing 4:, data access object interface

//...import statement
public interface ContactDao {
    public List<Contact> findAll();
    public Contact save(Contact contact);
    public void delete(Contact contact);

Listing 5:, ContactDao implemenation

//...import statements
@Repository(value = "contactDao")
public class ContactDaoImpl implements ContactDao {  
    private final Log log = LogFactory.getLog(ContactDaoImpl.class);
    private SessionFactory sessionFactory;    
    public List<Contact> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Contact c").list();
    public Contact save(Contact contact) {
        sessionFactory.getCurrentSession().saveOrUpdate(contact);"Contact Saved..." + contact.getId());
        return contact;
    public void delete(Contact contact) {
        sessionFactory.getCurrentSession().delete(contact);"Contact removed..." + contact.getId());

Listing 6:, from where we test our application

//...import statements
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring.xml");
        ContactDao contactDao = ctx.getBean("contactDao", ContactDao.class);
        Contact c[] = new Contact[5];
        c[0] = new Contact("Andrew", "Jones", new Date(), "", "123456789");
        c[0].addAddress(new Address("111 ABC Street", "LA", "US", "112233"));
        c[0].addAddress(new Address("222 ERT Street", "CA", "US", "445566"));
        c[0].addAddress(new Address("333 PPP Street", "LA", "US", "667788"));
        c[1] = new Contact("Peter", "Parker", new Date(), "", "654213688");
        c[1].addAddress(new Address("999, XYZ street", "SOMEWHERE", "UK", "223344"));
        c[1].addAddress(new Address("888, KKK street", "ANYWHERE", "UK", "776644"));
        c[2] = new Contact("Lee", "Falk", new Date(), "", "73645542");
        c[2].addAddress(new Address("555, QPQ street", "NOWHERE", "IN", "776655"));
        c[2].addAddress(new Address("444, WIW street", "INWHERE", "IN", "775544"));
        c[3] = new Contact("Arun", "Roy", new Date(), "", "617864872");
        c[3].addAddress(new Address("767, TTT street", "DL", "IN", "667788"));
        c[3].addAddress(new Address("898, YYY street", "MB", "IN", "554477"));
        c[4] = new Contact("Simrit", "Ahuja", new Date(), "", "78766542");
        c[4].addAddress(new Address("90, ZZZ street", "GK", "SA", "339988"));
        for (int i = 0; i < 5; i++) {
        List<Contact> contacts = contactDao.findAll();        
        for (Contact con : contacts) {
            Set<Address> addresses = con.getContactAddress();            
            int i=1;
            for (Address a : addresses) {
                System.out.printf("Address %d: %s", i, a.toString());

Snapshot of the project output from Netbeans
Fig 1: Snapshot of the project output from Netbeans


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!

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved