【问题标题】:Importing hibernate configuration file into Spring applicationContext将休眠配置文件导入Spring applicationContext
【发布时间】:2012-05-30 15:51:32
【问题描述】:

我正在尝试将 Hibernate 3 与 Spring 3.1.0 集成。问题是应用程序无法找到在 hibernate.cfg.xml 文件中声明的映射文件。最初的 hibernate 配置有数据源配置、hibernate 属性和映射 hbm.xml 文件。 主 hibernate.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"/>

弹簧配置是:

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

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

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

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

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

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

    如果您的 master.hibernate.cfg.xml 在您的资源中,则此方法有效。

    【讨论】:

      【解决方案2】:

      您可以尝试使用以下代码将 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>
      

      【讨论】:

        猜你喜欢
        • 2013-04-26
        • 2018-12-16
        • 2017-04-30
        • 1970-01-01
        • 2014-11-05
        • 2021-12-17
        • 1970-01-01
        • 2016-11-06
        • 2011-05-03
        相关资源
        最近更新 更多