Java Reference
In-Depth Information
Listing C-6.
Configuring Hibernate Purely from Spring
<bean id="sampleSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="sampleDataSource"/>
<property name="mappingResources">
<list>
<value>com/hibernatebook/spring/Paper.hbm.xml</value>
<value>com/hibernatebook/spring/Article.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.pool_size">0</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
Note that in Listing C-6, purely in order to demonstrate the use of mapping files
in a
LocalSessionFactoryBean
configuration, we omit the specification of a Hibernate
AnnotationConfiguration
for the
configurationClass
property, causing it to default to
the normal (mapping file-based) Hibernate
Configuration
object.
Typically, the mappings themselves are specified in the conventional Hibernate manner
through XML mapping files or Java annotations. It would be entirely possible to arrange to
configure these externally, but no default Spring classes are provided to achieve this, and it is
difficult to see any obvious benefit that would accrue from such an approach.
Using Hibernate in Your Spring Beans
With your session factory configured as a Spring bean, you can now go on to create DAOs that
take advantage of Hibernate's functionality. Here, Spring really starts to come into its own, as it
provides you with the invaluable
HibernateDaoSupport
class to form the basis of your DAOs
(see Listing C-7).
Listing C-7.
Declaring the Interface for Our DAO
package com.hibernatebook.spring.dao;
import java.util.List;
import com.hibernatebook.spring.Article;
import com.hibernatebook.spring.Paper;
Search WWH ::
Custom Search