Hibernate load mapping of personaly class loader
-
13-06-2021 - |
Question
I have a problem with java jdk1.6.0_17 and hibernate . I must open a hibernate session factory but mapping file (mapper-test.jar) is load by a 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();
the problem is:
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)
the 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>
if load the mapper-test with build path is success, so the mapper-test is ok.
Seems that the Configuration search the file it/sigeco/infoweb/dao/app/IcFabb.hbm.xml in the system classloader and not in the URLClassLoader, but how do I connect the Configuration with the Loader?
Thank you
Solution
I've found one solution. I create a personaly ClassLoader, MapperClassLoader, that extend UrlClassLoader where in costructor:
MapperClassLoader(URL[] urls)
{
super( urls,MapperClassLoader.class.getClassLoader());
}
where urls contains the jars url. So set the parent loader whit the system class loader. After create MapperClassLoader, set the system ClassLoader whit MapperClassLoader
Thread.currentThread().setContextClassLoader( MapperClassLoader.getInstance());
So the mapper-test.jar is usabile.