Library Versions
This tutorial will be using Spring 3, Hibernate 4 and MySQL 5.
Maven Dependencies
Add the following dependencies into your pom.xml
MySQL
This is the JDBC driver, which is needed to connect to the MySQL database.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
commons-dbcp
This is the connection pool library.
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
hibernate
Hibernate is the ORM library. We are using version 4.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.0.Final</version>
</dependency>
Spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
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.
Person.java
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.
Person.hbm.xml
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"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true">
<class name="Person" table="Person">
<id name="id">
<generator class="native" />
</id>
<property name="firstName"/>
<property name="lastName"/>
</class>
</hibernate-mapping>
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) {
sessionFactory.getCurrentSession().save(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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- connection pool -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<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"/>
</bean>
<!-- hibernate config -->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- our dao -->
<bean id="dao" class="Dao">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
<!-- @Transactional support -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
</beans>
Run it
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Dao dao = (Dao) ctx.getBean("dao");
Person p = new Person();
p.setFirstName("Joe");
p.setLastName("Bloggs");
dao.savePerson(p);
The above will:
- Tell Spring to instantiate the beans
- Get a reference to the Dao
- Save a new Person into the database.