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.
<hibernate-configuration>
<session-factory>
...
<!-- For Hibernate XML mappings -->
<mapping resource="com/apress/springenterpriserecipes/course/
➥
Course.hbm.xml" />
</session-factory>
</hibernate-configuration>
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="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.
LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="courseDao"
class="com.apress.springenterpriserecipes.course.hibernate.
➥
HibernateCourseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
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
container.
package com.apress.springenterpriserecipes.course;
...
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Search WWH ::
Custom Search