• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1117
  • Last Modified:

spring hibernate LocalSessionFactoryBean



I was reading folowing lines. And I did not understand clearly.

SessionFactory needs to know to which database to connect. The
preferred way to do this is to wire a DataSource to the LocalSessionFactoryBean:
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/trainingDatasource</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
You also manage how Hibernate is configured through the same LocalSession-
FactoryBean bean. Hibernate itself has dozens of properties by which you can
tweak its behavior. When used outside of Spring, Hibernate looks for a file named
hibernate.properties somewhere on the application class path for its configurations.
However, with Spring you do not have to manage these configurations in a
separate properties file. Instead, you can wire them to the hibernateProperties
property of the LocalSessionFactoryBean:
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.
dialect.MySQLDialect</prop>
</props>
</property>

</bean>
One last thing you must configure is which mapping files Hibernate should
read in. Remember when we created a Student.hbm.xml file? Well, we actually
have to tell Hibernate it needs to use this file. Otherwise it will not know how to
map the Student class to the database. Again, we can configure this through a
property of the LocalSessionFactoryBean bean. In this case, we use the mapping-
Resources property:
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">

For configuring DataSource  where are we giving host name, username, password etc details. what is LocalSessionFactoryBean. what are its uses.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
  • 3
  • 3
  • 2
2 Solutions
 
for_yanCommented:
What this paragrph explains is that if you use Hibernate with Spring then you configure
it using LocalSessionFactoryBean bean which needs to have two properties - one is called
"hibernateProperties" which configures different properties which define behaviour of Hibernate, another property is
"mappingResources" which will normally specify the xml files which contain information fo how specific
classes will be mapped to database,
I think it will be more obvious if you look at specific examples of how it is done say on this page:

http://www.shoesobjects.com/blog/2004/11/21/1101083542880.html
(go a ittle bit down to xml file after Java codes)

or on this page:

http://forum.springsource.org/showthread.php?t=71211

0
 
objectsCommented:
> For configuring DataSource  where are we giving host name, username, password etc details. what is LocalSessionFactoryBean. what are its uses.

LocalSessionFactoryBean does not have to use a DataSource, you can either configure it to use a DataSource or have it manages create the connections itself.
0
 
gudii9Author Commented:
>>use Hibernate with Spring then you configure
it using LocalSessionFactoryBean bean


you mean we need to configure LocalSessionFactoryBean to use hibernate in spring right. please advise
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:


Yes, you can see example of it here
http://www.shoesobjects.com/blog/2004/11/21/1101083542880.html

look wt the sixth box with code counting from top (or second from the bottom).
0
 
gudii9Author Commented:
>>>  <property name="mappingResources">
            <list>
                <value>MyRecord.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
        </property



i thought there should be hibernateProperties under hibernateProperties similar to mappingResource. please advise
0
 
gudii9Author Commented:
i mean property files.
0
 
for_yanCommented:
No, mappingRessources have xml files
which rrespond to mapping classes
and hibernateProperties are general properties
which are given explicitly below
0
 
objectsCommented:
> you mean we need to configure LocalSessionFactoryBean to use hibernate in spring right. please advise

you don't have to, just makes it easier

> i thought there should be hibernateProperties under hibernateProperties similar to mappingResource. please advise

you can specify the properties directly (instead of using a properties file)
eg.

            <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>

this is the same as specify in your properties files

   hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect

Just means one less config file to manage, and keeps everything in one spot


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now