9

How do you configure the Hibernate class mappings of class org.springframework.orm.hibernate3.LocalSessionFactoryBean in the Spring application context? I want to move the session factory class mappings from the following hibernate.cfg.xml to the corresponding Spring session factory bean so that I may eliminate hibernate.cfg.xml.

File hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- ... -->
        <mapping resource="Queries.hbm.xml" />
        <mapping class="com.company.app.common.model.Account" />
        <mapping class="com.company.app.common.model.AccountCategory" />
        <mapping class="com.company.app.common.model.AssetType" />
        <mapping class="com.company.app.common.model.Book" />
        <mapping class="com.company.app.model.AssetTypeCategory" />
        <!-- ... -->
    </session-factory>
</hibernate-configuration>

File spring-application-context.xml:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

    <!-- Instead of the above, I want to use the following. Where and
    how do I define the class mappings so that I may eliminate
    hibernate.cfg.xml? -->
    <--
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>Queries.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
    -->
</bean>
3
  • I want to eliminate the hibernate.cfg.xml file by moving its contents into the Spring application context. Mar 21, 2011 at 15:28
  • Are these annotated entity classes?
    – skaffman
    Mar 21, 2011 at 15:29
  • Yes, these are annotated classes. Mar 21, 2011 at 15:30

3 Answers 3

26

If you're using JPA-annotated classes, you can use AnnotationSessionFactoryBean instead of LocalSessionFactoryBean, and inject the classes directly into the Spring bean:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
       <list>
           <value>com.company.app.common.model.Account</value>
           <value>com.company.app.common.model.AccountCategory</value>
           <value>com.company.app.common.model.AssetType</value>
           <value>com.company.app.common.model.Book</value>
           <value>com.company.app.model.AssetTypeCategory</value>      
       </list>
    </property>
    <property name="mappingResources">
       <list>
          <value>Queries.hbm.xml</value>
       </list>
    </property>        
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
</bean>
2
  • Where do I place Queries.hbm.xml which contains some HQL queries? Mar 21, 2011 at 15:38
  • @Derek: AnnotationSessionFactoryBean extends LocalSessionFactoryBean, so you can still use all the same properties as before, including mappingResources.
    – skaffman
    Mar 21, 2011 at 15:39
9

As a slight variation on skaffman's answer, I used property packagesToScan of class AnnotationSessionFactoryBean to avoid listing all of the individual model class names:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
       <list>
           <value>com.company.app.common.model</value>
       </list>
    </property>
    <property name="mappingResources">
       <list>
          <value>Queries.hbm.xml</value>
       </list>
    </property>        
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
</bean>

I learned about this property from the excellent book Spring in Action, Third Edition.

0

I was also facing the same issue, and this worked for me -

<bean id="auditCoreSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="auditCoreDataSource" />
    </property>
    <property name="packagesToScan" value="com.odeon.audit.dao.entity" />
    <property name="annotatedClasses">
        <list>
            <value>com.odeon.audit.dao.entity.AuditLogEntity</value>
            <value>com.odeon.audit.dao.entity.AuditLogApplicationEtity</value>
            <value>com.odeon.audit.dao.entity.AuditLogModuleEntity</value>
            <value>com.odeon.audit.dao.entity.AuditLogOperationEntity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">#{auditCoreProp.getString('jdbc.dialect')}</prop>
            <prop key="hibernate.show_sql">#{auditCoreProp.getString('jdbc.show_sql')}</prop>
            <prop key="hbm2ddl.auto">#{auditCoreProp.getString('jdbc.hbm2ddl.auto')}
            </prop>
            <!-- prop key="hibernate.hbm2ddl.auto">create</prop -->
        </props>
    </property>
</bean>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.