Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hibernate with Spring

Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I though it may useful to some body who is looking for such information...
As most of us well aware of Hibernate and Spring frameworks, I am not getting into much of introduction part. However in short,

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

� 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 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.

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

Defining a Session Factory: Hibernate session factory can be defined using LocalSessionFactoryBean 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.

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

It might be a quick, not much detailed enough as I covered only basic configuration. Please let write your inputs/comments, I will try to add some more details�..
[ June 26, 2008: Message edited by: Yellappa Adepu ]
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add hibernate to my existing spring application.I am using spring 2.5,hibernate annotations 3.2, mysql and tomcat
I have written applicationcontext.xml and hibernate.cfg.xml.
But I am getting this error

Context initialization IOException parsing XML document from servletcontext resource [/web-inf/applicationcontext.xml]
nested exception is could not open servletcontext resource [/web-inf/applicationContext.xml]

Below are the two config files


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="";
<!-- the parent application context definition for the springapp application -->

<bean id="dataSource"
<property name="driverClassName">
<property name="url">
<property name="username">
<property name="password">

<bean id="sessionFactory"
<property name="dataSource">
<ref bean="dataSource" />
<property name="configLocation">
<property name="configurationClass">
<property name="hibernateProperties">
<prop key="hibernate.dialect">
<prop key="">create</prop>
<bean id="customerDAOimpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />

<bean id="customerDAO" class=""
<property name="sessionFactory"><ref local="sessionFactory"/></property>



<mapping package="" />
<mapping class="" />



Yellapa Adepu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your applicationContext.xml located. If it is not there under WEB-INF, you may encounter the described error.

1. Try to move the applicationContext.xml under WEB-INF folder.
2. Use classpath expression from where your loading the applicationContext.xml. eg.

Let me know if I can be more helpful.

Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic