• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring And Hibernate (LocalSessionFactoryBean)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic