如何将Hibernate类映射转换为Spring应用程序上下文? [英] How do you translate Hibernate class mappings to a Spring application context?

查看:157
本文介绍了如何将Hibernate类映射转换为Spring应用程序上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置类 Hibernate 类映射/docs/3.0.x/api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html\">org.springframework.orm.hibernate3.LocalSessionFactoryBean 中的 Spring 应用程序上下文?我想将会话工厂类映射从以下 hibernate.cfg.xml 到相应的Spring会话工厂bean,这样我可以消除 hibernate.cfg.xml

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.

档案 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>

文件 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>


推荐答案

如果您使用JPA注释的类,可以使用 AnnotationSessionFactoryBean 而不是 LocalSessionFactoryBean ,直接将类注入Spring bean:

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>

这篇关于如何将Hibernate类映射转换为Spring应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆