有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将hibernate配置文件导入Spring applicationContext

我正在尝试将Hibernate3与Spring3.1.0集成。问题是应用程序无法找到在hibernate中声明的映射文件。cfg。xml文件。最初,hibernate配置有数据源配置、hibernate属性和映射hbm。xml文件。 冬眠大师。cfg。xml文件存在于src文件夹中。主文件的外观如下所示:

<hibernate-configuration>
    <session-factory>
        <!-- Mappings -->
        <mapping resource="com/test/class1.hbm.xml"/>
        <mapping resource="/class2.hbm.xml"/>
        <mapping resource="com/test/class3.hbm.xml"/>
        <mapping resource="com/test/class4.hbm.xml"/>
        <mapping resource="com/test/class5.hbm.xml"/>

Spring配置为:

<bean id="sessionFactoryEditSolution" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="data1"/>
        <property name="mappingResources">
            <list>
                <value>/master.hibernate.cfg.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>

            </props>
        </property>
    </bean>

共 (2) 个答案

  1. # 1 楼答案

    您可以尝试使用下面的代码将spring指向hibernate的正确位置。cfg。xml

    <bean
        id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    
        <property name="configLocation">    
            <value>
                classpath:location_of_config_file/hibernate.cfg.xml
            </value>
        </property>
    ...........
    </bean>
    
  2. # 2 楼答案

    在路径的开始处有一个正斜杠,因此在根中查找它。这几乎肯定是不正确的

    <value>/master.hibernate.cfg.xml</value>
    

    我通常会这样指定我的配置:

    <value>classpath:master.hibernate.cfg.xml</value>
    

    如果你的master.hibernate.cfg.xml在你的资源中,这是可行的