0

java jdk1.6.0_17 和 hibernate 有问题。我必须打开一个休眠会话工厂,但映射文件(mapper-test.jar)是由个人类加载器(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>

如果使用构建路径加载映射器测试是成功的,那么映射器测试就可以了。

似乎配置在系统类加载器中而不是在 URLClassLoader 中搜索文件 it/sigeco/infoweb/dao/app/IcFabb.hbm.xml,但是如何将配置与加载器连接?

谢谢

4

1 回答 1

0

我找到了一种解决方案。我创建了一个个性化的 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 是可用的。

于 2012-06-12T15:01:45.307 回答