Sunday, January 31, 2010

Integrating Hibernate with Spring

Introduction

As most of us well aware of Hibernate and Spring frameworks, I am not getting into much of introduction part. However in short,

Hibernate

  • A popular & successful ORM tool
  • Abstracting the underlining JDBC programming
  • Portable to any supported database with minor configuration changes
  • Provides Transaction Management etc….

Spring

  • Implementation Inversion of Control (IOC) design pattern.
  • Abstraction to Aspect Oriented Programming (AOP).
  • Creates the java object beans from an application context xml (spring configuration file)

I would like to walk you through a running example how they both work together… Please take a look at the below xml..

Header part of spring configuration xml includes spring-bean.dtd


Loading Hibernate properties: Hibernate properties can be loaded using Spring’s PropertyPlaceholderConfigurer class. Provide hibernate.properties file location as value. If the file placed under any other directory other than WEB-INF directory, use classpath: expression to let the Spring to look for the file in available classpath.





classpath:/properties/hibernate.properties


Defining a DataSource: Use JndiObjectFactoryBean class to define the data source by providing JNDI binding name.




jdbc/SampleDS

Defining a Session Factory: Hibernate session factory can be defined usingLocalSessionFactoryBean class, provides an integration point to Hibernate from Spring.

Here we need to provide data source information and hibernate mapping files (.hbm.xml).

Transaction factory class, hibernate supports

org.hibernate.transaction.JDBCTransactionFactory – delegates to database (JDBC) transactions (default)

org.hibernate.transaction.JTATransactionFactory – delegates to container-managed transaction if an existing transaction is underway in this context (e.g. EJB session bean method), otherwise a new transaction is started and bean-managed transaction are used.

org.hibernate.transaction.CMTTransactionFactory – delegates to container-managed JTA transactions

Transaction Lookup class: It depends on application server on which the application is running, here I am using WebSphere Application Server related one.

Special columns if there are any column defined CLOBs, those can handled using default implementation provided spring. Here as an example I am using CLOB handler.







com/sample/TableName1.hbm.xml




${hibernate.dialect}
${hibernate.show_sql}
${hibernate.default_schema}

org.hibernate.transaction.JTATransactionFactory

org.hibernate.transaction.WebSphereTransactionManagerLookup
java:comp/UserTransaction
org.hibernate.cache.EhCacheProvider
true
true
false







Defining a Transaction Manager: By providing the above defined session factory, a transaction manager needs to be defined as below.
























PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED,readOnly


It might be a quick, not much detailed enough as I covered only basic configuration. Your comments / inputs are much appreciated... I will try to add some more details…..