How do you translate Hibernate class mappings to a Spring application context?

前端 未结 3 672
滥情空心
滥情空心 2021-02-02 03:12

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 sess

相关标签:
3条回答
  • 2021-02-02 03:27

    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 讨论(0)
  • 2021-02-02 03:34

    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>
    
    0 讨论(0)
  • 2021-02-02 03:35

    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>
    
    0 讨论(0)
提交回复
热议问题