Search This Blog

Wednesday, September 17, 2008

Spring & HIbernate

Here I would like to recommend the best combination which is Spring with Hibernate for an Enterprise application which confirms to OOPS philosophy.

Before we go in-depth about the topic, some facts about using an ORM:

Advantages:
Speeds-up Development - eliminates the need for repetitive SQL code.
Reduces Development Time.
Reduces Development Costs.
Overcomes vendor specific SQL differences - the ORM knows how to write vendor specific SQL so you don't have to.

Disadvantages:
Loss in developer productivity whilst they learn to program with ORM.
Developers loose understanding of what the code is actually doing - the developer is more in control using SQL.
ORM has a tendency to be slow.
ORM fail to compete against SQL queries for complex queries.

For Hibernate, Spring framework provides first-class support with lots of IoC convenience features, addressing many typical Hibernate integration issues. All of these support packages for O/R (Object Relational) mappers comply with Spring's generic transaction and DAO exception hierarchies. There are usually two integration styles: either using Spring's DAO 'templates' or coding DAOs against plain Hibernate/JDO/TopLink/etc APIs.

Spring framework is based on Java Bean configuration management with Inversion of control principle (IoC). Spring uses its IoC container as the principal building block for a comprehensive solution that reflects all architectural features. Its unique data access system with a simple JDBC framework improves its productivity with less error. Its AOP program written in standard Java provides better transaction management services and also enables it for different applications.

Following are the modules of the Spring Core Container:
Beans, Core, Context, Expression Language

Dependencies are satisfied through the following:

1. Constructor Injection
2. Setter Injection
3. Interface Injection

Configure the spring bean configuration file
Create bean entries for the following:
jsp view resolver, datasource, sessionfactory & other domain specific dao & controller beans.

Hibernate Mapping for the DAO object will be in the .hbm.xml file

Use a seperate DAO class to interact with the database.
Use a MultiActionController class to handle the web requests.
Add hibernate annotations to Bean classes, if you want to add any database related constraints.
Example:

@Id
@GeneratedValue
@Column(name="USER_ID")
public Long getId() {
   return id;
}

Ensure all DAO classes implement an interface so that save & retrieve methods can be implemented on them.
Use Hibernate Template to access the database in the DAO object.
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.saveOrUpdate(user);
return hibernateTemplate.find("from User");

Hibernate Template is thread safe and reusable. You need not manually open and close Session, Hibernate Template will do that for you.

The specific say User Controller class will extend MultiActionController class. The UserDAOImpl instance is injected using setter injection.

In the jsp page we use Spring Form tags to display the form fields and jstl tags to display the list of users. In the add method we call the saveUser() method and redirect the control to the "list.htm" url. This will invoke the list() method. In the list method you add two things to the modelMap, the user list to display the list of users and an instance of the user object to bind the form fields in the userForm.jsp page.

In the jsp page we use Spring Form tags to display the form fields and jstl tags to display the list of users.