Spring - Hibernate如何从类路径资源中加载*.hbm.xml文件

4

我在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。请告诉我我做错了什么。 谢谢。

类似于WAR有两个META-INF文件夹 - dma_k
5个回答

4

当使用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>

3
@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;
}

1

我认为这看起来还不错。因此,我不认为问题在于配置文件。我更倾向于认为文件只是没有在类路径上。你是如何启动应用程序的?

如果你使用的是Eclipse,请确保将src/main/resources用作源文件夹,并将资源复制到target/classes。


是的,我将默认的Maven src/main/resources作为源文件夹,并且我的应用程序是Web应用程序。我已经将这些文件复制到WEB-INF/classes文件夹中,但它不起作用。 - robinmag
当你说“这些文件到web-inf/classes”时,你是指例如web-inf/classes/SystemUser.hbm.xml吗?如果是的话,那么你应该将它移动到web-inf/classes/mapping/SystemUser.hbm.xml。我现在没有其他想法。 - sfussenegger

0
在Web应用程序中,如果您编写没有前缀的资源路径,则Spring将从上下文根(即包含WEB-INF的文件夹)加载它。要从类路径加载资源,您应该使用"classpath:"前缀:
<value>classpath:mapping/SystemUser.hbm.xml</value>

0
如果您正在从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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接