Sunday, April 1, 2012

Spring: Spring ROM Support for Hibernate with MySQL

Overview:
This article looks at how you can use Spring3 with Hibernate3. This is a simple example that shows how you can integrate Spring with Hibernate. Also, I have used Maven3 to build the project. You can find the code here.

Prerequisite:
MySQL
Maven

Project Structure:This is how the project structure looks like (Click on the image to view).



Step 1: Creating the Database Table
 CREATE TABLE PERSON (
PERSON_ID BIGINT AUTO_INCREMENT,
FIRST_NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
PRIMARY KEY (PERSON_ID)
)
Step 2: Creating the POJO Class

public class Person implements Serializable {
private Long id;
private String firstName;
private String lastName;
public Person() {
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "[" + id + ", " +firstName+ ", " + lastName+ "]";
}
}
Step 3: Mapping the Person POJO with Hibernate.
Create resources/org/fazlan/spring/hibernate/hbm/Person.hbm.xml
 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.fazlan.spring.hibernate.pojo.Person" table="PERSON">
<id name="id" column="PERSON_ID" type="long" access="field">
<generator class="increment" />
</id>
<property name="firstName" type="string">
<column name="FIRST_NAME" />
</property>
<property name="lastName" type="string">
<column name="LAST_NAME" />
</property>
</class>
</hibernate-mapping>
Step 4: Creating the DAO and BO Classes
Implementation of DAO and BO.

DAO Interface:
public interface IDao<T> {
Serializable save(T t);
void update(T t);
void delete(T t);
T get(Serializable id);
}
DAO Implementation:
public class PersonDao extends HibernateDaoSupport implements IDao<Person>{
public Long save(Person person) {
return (Long) getHibernateTemplate().save(person);
}
public void update(Person person) {
getHibernateTemplate().update(person);
}
public void delete(Person person) {
getHibernateTemplate().delete(person);
}
public Person get(Serializable id) {
return (Person) getHibernateTemplate().get(Person.class, id);
}
}
BO Interface:
 public interface IService<T> {
Serializable save(T t);
void update(T t);
void delete(T t);
T get(Serializable id);
}
BO Abstract implementation using generics:
 public abstract class AbstractService<T> implements IService<T> {
private IDao dao;
public Serializable save(T t) {
return dao.save(t);
}
public void update(T t) {
dao.update(t);
}
public void delete(T t) {
dao.delete(t);
}
public T get(Serializable id) {
return (T) dao.get(id);
}
public void setDao(IDao dao) {
this.dao = dao;
}
}
BO implementation for Person:

@Override
public class PersonService extends AbstractService<Person>{
public Long save(Person person) {
return (Long) super.save(person);
}
} 
Step 5: Configuring Database properties.
Create the properties file resources/properties/database.properties
 jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springhibernate
jdbc.username=root
jdbc.password=root123
Step 6: Wiring the DAO and BO using Spring Configuration.
Configuring the datasource and the seessionfactory.
Create file resources/org/fazlan/spring/hibernate/spring/spring-conf-dao.xml
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>properties/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>org/fazlan/spring/hibernate/hbm/Person.hbm.xml</value>
</list>
</property>
</bean>
</beans>
Wiring the DAO and BA entities.
Create file resources/org/fazlan/spring/hibernate/spring/spring-conf-person.xml
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personDao"
class="org.fazlan.spring.hibernate.dao.PersonDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="personService"
class="org.fazlan.spring.hibernate.service.PersonService">
<property name="dao" ref="personDao"/>
</bean>
</beans>
Combining all the configuration into a single file (as a best practice).
Create file resources/org/fazlan/spring/hibernate/spring/spring-conf-application.xml
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="spring-conf-dao.xml" />
<import resource="spring-conf-person.xml" />
</beans>
Step 7: Create an application to test the functionality.
 public class Main {
 public static void main(String... args) {
   ApplicationContext context =
       new ClassPathXmlApplicationContext("org/fazlan/spring/hibernate/spring/spring-conf-application.xml");
   IService service = (IService) context.getBean("personService");
   Person p1 = new Person("Fazlan" , "Sabar");
   Serializable id = service.save(p1);
   p1 = (Person) service.get(id);
   p1.setFirstName("Updated FirstName");
   p1.setLastName("Updated LastName");
   service.update(p1);
 }
}
You can find the code here.


Summary:
This article briefed how you can use Spring's ROM support to integrate with Hibernate.

No comments:

Post a Comment