Setting up a Spring, Hibernate and MySQL project

Published: Saturday, 27 April 2013

Library Versions

This tutorial will be using Spring 3, Hibernate 4 and MySQL 5.

Maven Dependencies

Add the following dependencies into your pom.xml


This is the JDBC driver, which is needed to connect to the MySQL database.



This is the connection pool library.



Hibernate is the ORM library. We are using version 4.




Domain class and mapping

Create a domain class to be persisted. In this example I have a class named Person, which will be mapped into the Person table.

public class Person {
  private Long id;
  private String firstName;
  private String lastName;

I’ve omitted the package name and getters and setters in this example.


Add a corresponding hibernate mapping. An alternative would be to annotate the Java class with mapping information.

<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping auto-import="true">
  <class name="Person" table="Person">
    <id name="id">
        <generator class="native" />
    <property name="firstName"/>
    <property name="lastName"/>

There is a new 4.0 xsd schema that comes with hibernate, but I wasn’t able to figure out how to use it.

Create a DAO

The @Transactional annotation is used when you need a connection to the database.

import org.hibernate.SessionFactory;

public class Dao {
    private SessionFactory sessionFactory;

    @Transactional(readOnly = false)
    public void savePerson(Person p) {

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;

Spring context

Create your application context spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       xmlns:xsi="" xmlns:tx=""
  <!-- connection pool -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/example"/>
    <property name="username" value="example"/>
    <property name="password" value="example"/>
    <property name="testOnBorrow" value="true"/>
    <property name="maxActive" value="10"/>
    <property name="minIdle" value="0"/>
    <property name="minEvictableIdleTimeMillis" value="60000"/>
    <property name="validationQuery" value="select 1 from dual"/>

  <!-- hibernate config -->
  <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
    <property name="hibernateProperties">
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>

  <!-- our dao -->
  <bean id="dao" class="Dao">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>

  <!-- @Transactional support -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernateSessionFactory"/>


Run it

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Dao dao = (Dao) ctx.getBean("dao");
Person p = new Person();

The above will:

  1. Tell Spring to instantiate the beans
  2. Get a reference to the Dao
  3. Save a new Person into the database.