Java Reference
In-Depth Information
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
Now let's see how to declare a session factory that uses XML mapping files in Spring. For this
purpose, you have to enable the XML mapping file definition in hibernate.cfg.xml again.
<!-- For Hibernate XML mappings -->
<mapping resource="com/apress/springenterpriserecipes/course/
Course.hbm.xml" />
Then you create a bean configuration file for using Hibernate as the ORM framework (e.g.,
beans-hibernate.xml in the classpath root). You can declare a session factory that uses XML mapping
files with the factory bean LocalSessionFactoryBean . You can also declare a HibernateCourseDao instance
under Spring's management.
<beans xmlns=""
xsi:schemaLocation=" ">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3. LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<bean id="courseDao"
<property name="sessionFactory" ref="sessionFactory" />
Note that you can specify the configLocation property for this factory bean to load the Hibernate
configuration file. The configLocation property is of type Resource , but you can assign a string value
to it. The built-in property editor ResourceEditor will convert it into a Resource object. The preceding
factory bean loads the configuration file from the root of the classpath.
Now you can modify the Main class to retrieve the HibernateCourseDao instance from the Spring IoC
package com.apress.springenterpriserecipes.course;
import org.springframework.context.ApplicationContext;
Search WWH ::

Custom Search