• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Hibernate with Spring

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
I though it may useful to some body who is looking for such information...
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.



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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
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 failed.org.springframework.beans.factory.beandefinitionstoreexception: IOException parsing XML document from servletcontext resource [/web-inf/applicationcontext.xml]
nested exception is java.io.FilenotfoudException: could not open servletcontext resource [/web-inf/applicationContext.xml]


Below are the two config files

applicationContext.xml
****************************

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- the parent application context definition for the springapp application -->

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/bank</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.
LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>WEB-INF\classes\hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="customerDAOimpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="customerDAO" class="com.palnar.college.DAO.customerDAOHibernateImpl.java"
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


</beans>

hibernate.cfg.xml
************************

<hibernate-configuration>
<session-factory>
<mapping package="com.palnar.college.service" />
<mapping class="com.palnar.college.service.Customer" />
</session-factory>
</hibernate-configuration>

web.xml
*************
......
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

......

PLEASE HELP ME OUT.THIS IS VERY URGENT
 
Yellapa Adepu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
OR
2. Use classpath expression from where your loading the applicationContext.xml. eg.


Let me know if I can be more helpful.

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic