我遇到java jdk1.6.0_17和hibernate的问题。 我必须打开一个hibernate会话工厂,但映射文件(mapper-test.jar)由personaly classLoader(URLClassLoader)加载
File map = new File("/opt/sigeco/infowebrepository/mapper/mapper-test.jar");
File map1 = new File("/opt/sigeco/infowebrepository/mapper/hibernate3.jar");
URL[] urls = new URL[] { map.toURI().toURL(),map1.toURI().toURL()};
URLClassLoader loader = new URLClassLoader(urls);
//test load is ok
loader.loadClass("it.sigeco.infoweb.dao.app.IcFabb");
Configuration configuration=new Configuration();
configuration.configure( loader.getResource("hibernate.cfg.xml"));
configuration.buildSessionFactory();
问题是:
org.hibernate.MappingException: Resource: it/sigeco/infoweb/dao/app/IcFabb.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1325)
at testLoad.main(testLoad.java:32)
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.jdbc.use_scrollable_resultset">true</property>
<mapping resource="it/sigeco/infoweb/dao/app/IcFabb.hbm.xml"/>
</session-factory>
</hibernate-configuration>
如果使用构建路径加载mapper-test成功,那么mapper-test就可以了。
似乎Configuration在系统类加载器中搜索文件it / sigeco / infoweb / dao / app / IcFabb.hbm.xml而不是在URLClassLoader中,但是如何将配置与Loader连接?
谢谢
答案 0 :(得分:0)
我找到了一个解决方案。我创建了一个personaly ClassLoader,MapperClassLoader,它在costructor中扩展了UrlClassLoader:
MapperClassLoader(URL[] urls)
{
super( urls,MapperClassLoader.class.getClassLoader());
}
其中urls包含jars url。因此,将父加载器设置为系统类加载器。 创建MapperClassLoader后,将系统ClassLoader设置为MapperClassLoader
Thread.currentThread().setContextClassLoader( MapperClassLoader.getInstance());
所以mapper-test.jar是可用的。