Welcome to my blog

I blog my interests and opinions here.

Monday, January 31, 2011

Using Spring and Hibernate makes developers life easier

A hibernate based application typically uses object relational mapping paradigm to map a database table
CREATE TABLE `hello` (
`id` int(11) NOT NULL,
`description` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
)

to an entity class in Java

package dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* Hello generated by hbm2java
*/
@Entity
@Table(name = "hello", catalog = "test")
public class Hello implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String description;

public Hello() {
}

public Hello(int id) {
this.id = id;
}

public Hello(int id, String description) {
this.id = id;
this.description = description;
}

@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

@Column(name = "description", length = 45)
public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

}
Spring uses an application-context.xml file that specifies the bean dependency injection configurations for the data source, hibernate session factory, hibernate template and the Data Access Object (DAO)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>hello.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>

<bean id="myHelloDao" class="dao.HelloDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>

</beans>

The mapping between the entity and the database table is specified in the hibernate mapping file hello.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="dto.Hello" table="hello" catalog="test">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="description" type="string">
<column name="description" length="45" />
</property>
</class>
</hibernate-mapping>

The Data Access Object (DAO) class would (uses the hibernate template callback mechanism in order to access the database) look like

public class HelloDao {

private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}

public HelloDao()
{

}


/**
* Gets all the rows from test.Hello table
* @return
*/
public List<Hello> getAll() throws DataAccessException
{
List<Hello> helloList = null;

try
{
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException,SQLException {
return session.createQuery("from Hello").list();
}
};
helloList = (List<Hello>)hibernateTemplate.execute(callback);
}
catch(Exception ex)
{
throw new DataAccessException("Error when getting all records.",ex);
}

return helloList;
}
}

Introducing Spring changes the way a typical hibernate application works by
1. Reducing the number of configuration files on the server side
2. Allowing multiple clients to connect to different databases (by injecting different data source configurations in the application-context.xml) without having to change the server side code.

No comments:

Post a Comment