【问题标题】:spring - hibernate load *.hbm.xml from classpath resourcespring - 从类路径资源休眠加载 *.hbm.xml
【发布时间】:2010-12-29 04:43:00
【问题描述】:

我在 src/main/resources maven 文件夹中的类路径资源中有一些 hbm.xml 文件。我使用 spring 的 LocalSessionFactoryBean 来加载这些文件,并使用以下 bean 配置:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceOracle"/>
    <property name="mappingResources">
        <list>
            <value>mapping/SystemUser.hbm.xml</value>
            <value>mapping/SystemCredential.hbm.xml</value>
            <value>mapping/SystemProvince.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
        </value>
    </property>
</bean>

但它给了我 FileNotFoundException。请告诉我我做错了什么 谢谢。

【问题讨论】:

标签: java hibernate spring maven-2 orm


【解决方案1】:

这对我来说看起来不错。因此,我认为问题不在于配置。我宁愿认为这些文件根本不在类路径上。你是如何开始你的申请的?

如果您使用的是 eclipse,请确保将 src/main/resources 用作源文件夹,并将资源复制到目标/类。

【讨论】:

  • 是的,我有默认 maven 的 src/main/resources 作为源文件夹,我的应用程序是 webapp。我已将这些文件复制到 web-inf/classes 文件夹,但它不起作用
  • 当你说“这些文件到 web-inf/classes”时,你的意思是例如web-inf/classes/SystemUser.hbm.xml?如果是,那么您应该将其移至web-inf/classes/mapping/SystemUser.hbm.xml。我现在没有任何其他想法。
【解决方案2】:

在 Web 应用程序中,当您编写不带前缀的资源路径时,Spring 会从上下文根(即,从包含 WEB-INF 的文件夹)加载它。要从类路径加载资源,您应该使用“类路径:”前缀:

<value>classpath:mapping/SystemUser.hbm.xml</value>

【讨论】:

    【解决方案3】:

    当使用 Maven 和 war 类型的项目时,位于 src/main/resources 的文件最终会在 WEB-INF/classes 中(并保留 resources 目录结构)。所以要么将你的映射文件放在src/main/resources/mapping 中,要么使用以下配置:

    <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSourceOracle"/>
            <property name="mappingResources">
                    <list>
                            <value>SystemUser.hbm.xml</value>
                            <value>SystemCredential.hbm.xml</value>
                            <value>SystemProvince.hbm.xml</value>
                    </list>
            </property>
            <property name="hibernateProperties">
            <value>
                    hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
            </value>
        </property>
    </bean>
    

    【讨论】:

      【解决方案4】:
      @Autowired
      private ResourceLoader rl;
      
      
      @Bean
      public LocalSessionFactoryBean sessionFactory() throws IOException {
          LocalSessionFactoryBean sessionFactoryBean = new   LocalSessionFactoryBean();
          sessionFactoryBean.setMappingLocations(loadResources());
      }
      
      public Resource[] loadResources() {
          Resource[] resources = null;
          try {
              resources = ResourcePatternUtils.getResourcePatternResolver(rl)
                      .getResources("classpath:/hibernate/*.hbm.xml");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return resources;
      }
      

      【讨论】:

        【解决方案5】:

        如果您从 web 应用加载 Spring 应用程序上下文,您可能会看到如下错误:

        SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
        org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: ServletContext resource [/hibernate.cfg.xml] cannot be resolved to URL because it does not exist
        

        解决方案是明确告诉 Spring 从类路径加载配置,如下所示:

        classpath:mypath/myfile.xml
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-07
          • 1970-01-01
          • 2011-04-22
          • 1970-01-01
          • 2011-03-27
          • 1970-01-01
          • 1970-01-01
          • 2010-10-26
          相关资源
          最近更新 更多