java spring - 从类路径资源休眠加载 *.hbm.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring - hibernate load *.hbm.xml from classpath resource
提问by robinmag
I have some hbm.xml files in classpath resource located in src/main/resources maven's folder. I used spring's LocalSessionFactoryBean to load these files with the following bean config:
我在位于 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>
But it gives me the FileNotFoundException. Please tell me what i've done wrong Thank you.
但它给了我 FileNotFoundException。请告诉我我做错了什么谢谢。
回答by Pascal Thivent
Files located in src/main/resourcesend up in WEB-INF/classeswhen using Maven with a project of type war(and the resourcesdirectory structure is preserved). So either place your mapping files in src/main/resources/mappingor use the following configuration:
将 Maven 与类型的项目一起使用时,src/main/resources最终位于 中的文件(并保留目录结构)。因此,要么将您的映射文件放入或使用以下配置:WEB-INF/classeswarresourcessrc/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>
回答by Syam Elakapalli
@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;
}
回答by sfussenegger
This looks quite okay to me. Hence I don't think the problem is the config. I rather think the files simply aren't on the classpath. How did you start your application?
这对我来说看起来还不错。因此,我认为问题不在于配置。我宁愿认为文件根本不在类路径上。你是如何开始你的申请的?
If you're using eclipse, make sure src/main/resources is used as source folder and resources are copied to target/classes.
如果您使用 eclipse,请确保将 src/main/resources 用作源文件夹,并将资源复制到目标/类。
回答by axtavt
In web applications, when you write a resource path without prefix, Spring loads it from a context root (i.e., from a folder containing WEB-INF). To load resources from a classpath you should use "classpath:" prefix:
在 Web 应用程序中,当您编写不带前缀的资源路径时,Spring 从上下文根(即,从包含 WEB-INF 的文件夹)加载它。要从类路径加载资源,您应该使用“classpath:”前缀:
<value>classpath:mapping/SystemUser.hbm.xml</value>
回答by Joe Khoobyar
If you are loading your Spring application context from a webapp, you might see an error like this:
如果您从 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
The solution is to explicitly tell Spring to load the configuration from the classpath like so:
解决方案是明确告诉 Spring 从类路径加载配置,如下所示:
classpath:mypath/myfile.xml

