wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Spring And Hibernate (LocalSessionFactoryBean) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Spring And Hibernate (LocalSessionFactoryBean)" Watch "Spring And Hibernate (LocalSessionFactoryBean)" New topic
Author

Spring And Hibernate (LocalSessionFactoryBean)

Meenakshi Lingan
Greenhorn

Joined: May 02, 2005
Posts: 4
Hi ,

I am trying to Integrate Spring and hibernate .

I have configured the ApplicationContext-Spring as follows.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<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/testdatabase</value>
</property>
<property name="username"><value>root</value></property>
<property name="password"><value>mysql</value></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>hyb/src/java/Honey.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> net.sf.hibernate.dialect.MySQLDialect </prop>
<!--prop key="hibernate.hbm2ddl.auto">create</prop-->
</props>
</property>
</bean>
</beans>




NOW,
1.I need a sample code to show how to use this LocalSessionFactoryBean -sessionfactory.

2.Is a hibernate.cfg.xml needed?-where we usualy point to the mapping xmls.

Thanks.
MeenakshiLingan.
Dave Turkel
Greenhorn

Joined: Sep 17, 2003
Posts: 9
I got this to work last week (so don't expect much help in followup questions), but here is what I know:

Everything I've seen seems to indicate that it's a bad idea to try to work with the sessionFactory directly, especially when you are going to need transaction support. I may be misreading (or don't know enough). Most people seem to use the HibernateTemplate, which I'll explain below.

Yes, of course you need a hibernate.cfg.xml. Spring is only abstracting Hibernate's components from your application for you. I found that you have to use the "configLocation" property of the sessionFactory for it to be pickedup:

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>

It will be located on your classpath. I specify my object mappings in separate files, indicated in the main hibernate.cfg.xml.

I found it easiest to use the HibernateDaoSupport and HibernateTemplate class for my Hibernate-related DAOs. HibernateTemplate (a Hibernate class) is a property of the HibernateDaoSupport class (a Spring class). When you define your DAO beans, you'll inject the HibernateTemplate bean into your DAO.

First, you need to define some more beans in your spring Context:


Then you need to create your HibernateDAO, e.g.:



Then you need to define the DAO beans in Spring context file. There are two, one that acts as a proxy (for transaction management). This is the one that you'll application will call. The second is the reference to the actual DAO class:



BTW, the Spring framework plugins for Eclipse (or at least JBoss IDE), are really useful to visualize what you have configured.
nishanth vincent
Greenhorn

Joined: Feb 13, 2009
Posts: 1
I have done something like this following eclipse's spring and hibernate tutorial...

I am using postgres database.. lets assume I am using 10 tables in my database. .
I am trying to load them using...

beanFactory = new XmlBeanFactory(new ClassPathResource(
“applicationContext.xml”));

my applicationContext.xml has the following ...

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-destroy-method="true" >

<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="hibernateSession"/></property>
</bean>



<bean id="LoginDAO" class="com.sendGift.app.hibernate.LoginDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
<bean id="LoginDAOService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target"><ref local="PersistenceLoginLayer"/></property>
</bean>
<bean id="PersistenceLoginLayer"
class="com.sendGift.app.hibernate.PersistenceLoginLayer"
abstract="false" scope="singleton" lazy-init="default"
autowire="default" dependency-check="default" >
<property name="loginDAO" >
<ref bean="LoginDAO" />
</property>
</bean>



<bean id="ContactDAO" class="com.sendGift.app.hibernate.ContactDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
<bean id="ContactDAOService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target"><ref local="PersistenceContactLayer"/></property>
</bean>
<bean id="PersistenceContactLayer"
class="com.sendGift.app.hibernate.PersistenceContactLayer"
abstract="false" scope="singleton" lazy-init="default"
autowire="default" dependency-check="default" >
<property name="contactDAO">
<ref bean="ContactDAO" />
</property>
</bean>
......
..........
</beans>

Now .. I am able to do all operations..like save update delete insert ..

The main problem is that .... when I load these 10 tables using my applicationContext.xml

There are 10 different process.. created in the task manager.. after going to all the process.. those 10 process .. are still there .. I think the connections are not closing down... for some reason ..
so when i open a new browser .. and try the same app.. another 10 postgres process adds up .. and so on ..
basically i need to close the connection ..

someone please help..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spring And Hibernate (LocalSessionFactoryBean)
 
Similar Threads
Getting Error DataSourceProxy
Hiberante transaction in Spring
Hibernate cannot open connection
spring transaction management only
Spring DAO and applicationConfig