Introduction to Spring ORM

In this tutorial, you are going to learn what Spring ORM is and how to use it.

java-featured-image

What is Spring ORM?

Spring ORM is covers many technologies like Hibernate, iBatis and JPA.  Spring provides integration classes thanks to which, each of the technologies mentioned are able to be implemented following the Spring principles of confiugration.

The recommended integration style is to make DAOs against Hibernate, JPA and JDO.

Because everything is designed as a set of reusable JavaBeans, we can extract just as much functionality from ORM support as we would extract from a library.

 Advantages of using ORM

Since we are mapping to a database table with a java object called “Entity”, we can interact with the environment using OOP concepts such as inheritance and encapsulation, and so on. This is a huge benefit because we are already familiar with OOP concepts and we won’t have to spend time learning new concepts, etc.

  • Easier testing
  • Common data access exceptions
  • General resource management
  • Integration transaction management

For fully in-depth insight of the above-mentioned advantages, click here for the documentation.




Implementation

First, we will need to set up SessionFactory in a Spring container. This is done to avoid tying application objects to hard-coded resource lookups. We define Hibernate SessionFactory as beans in an application context. If you are not familiar with beans, you can click here.

<beans>

  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
  </bean>

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

</beans>

The following example shows a DAO definition in a Spring container that references the above defined SessionFactory and an example for a DAO method implementation.

<beans>

  <bean id="myProductDao" class="product.ProductDaoImpl">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>

</beans>
public class ProductDaoImpl implements ProductDao {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    public Collection loadProductsByCategory(String category) throws DataAccessException {
    	return this.hibernateTemplate.find("from test.Product product where product.category=?", category);
    }
}

The above code snippets are taken from the original documentation.

What we did here is, we created a ProductDaoImpl clas that implements the ProductDao interface and within it, we have the HibernateTemplate instance variable alongside setSessionFactory() and loadProductsByCategory() methods.

As you can see, in the setSessionFactory() method, we set the hibernateTemplate to the specified sessionFactory.

What is HibernateTemplate?

HibernateTemplate is a class that helps simplifying the data access code. This class converts HibernateExceptions into DataAccessExceptions which is an unchecked exception. HibernateTemplate is used to implement data access or business logic services.

If you are interested in all the methods that ORM API supports, you can view the Oracle javadoc for it.

Leave a Reply

avatar