Hi,
I'm working on a software using Eclipse RCP and hibernate. I have a hibernate.cfg.xml and a set of mapping files xxx.hbm.xml.
When I run the app directly from Eclipse, all is fine: configuration and mapping files are loaded, parsed and I can access to the DB.
As for all RCP app one day, I wanted to deploy an alpha version for testing purposes. Once I have found how to load Hibernate (Buddy things), I managed to have my configuration file loaded. However, it fails to load the mapping files. I do not understand why as all these files are in the same JAR file: if it finds the configuration file it should be able to find the other files.
So I looked at the sources of Hibernate and there is a slight discrepency between the way the configuration file is loaded (configure which calls ConfigHelper.getResourceAsStream) and the way mapping files are (addResource which does slightly the same kind of stuff as ConfigHelper.getResourceAsStream but not exactly). See below for code snippets.
I am not expert on the class loader stuff so the code below is quite opaque to me...
So the questions:
Does anyone has a clue on why my mapping files can't be loaded when my configuration file can?
Subsidiary question: Why are configure and addResource methods not using the same way of loading resources (namely ConfigHelper.getResourceAsStream)?
Thx in advance
Regards
Hibernate version: 3.2.6
In org.hibernate.cfg.Configuration.java:
Code:
public class Configuration implements Serializable {
...
public Configuration addResource(String resourceName) throws MappingException {
log.info( "Reading mappings from resource : " + resourceName );
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
InputStream rsrc = null;
if (contextClassLoader!=null) {
rsrc = contextClassLoader.getResourceAsStream( resourceName );
}
if ( rsrc == null ) {
rsrc = Environment.class.getClassLoader().getResourceAsStream( resourceName );
}
if ( rsrc == null ) {
throw new MappingNotFoundException( "resource", resourceName );
}
try {
return addInputStream( rsrc );
}
catch (MappingException me) {
throw new InvalidMappingException( "resource", resourceName, me );
}
}
...
}
In org.hibernate.util.ConfigHelper.java:Code:
public final class ConfigHelper {
...
public static InputStream getResourceAsStream(String resource) {
String stripped = resource.startsWith("/") ?
resource.substring(1) : resource;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
return stream;
}
...
}
Full stack trace of any exception that occurs:Code:
!ENTRY org.eclipse.ui 4 0 2008-06-11 10:51:38.093
!MESSAGE Unhandled event loop exception
!STACK 0
org.hibernate.MappingNotFoundException: resource: org\toto\model\Environment.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at org.toto.app.util.Project.<init>(Project.java:76)
at org.toto.app.util.ProjectManager.openProject(ProjectManager.java:79)
at org.toto.app.actions.OpenProject.run(OpenProject.java:49)
at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:256)
at org.eclipse.ui.internal.WWinPluginAction.runWithEvent(WWinPluginAction.java:229)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:546)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:490)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:402)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3682)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3293)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2389)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2219)
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:289)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:461)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.toto.app.Application.start(Application.java:22)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:169)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
Debug level Hibernate log excerpt:Code:
10:51:37,906 INFO Environment:514 - Hibernate 3.2.6
10:51:37,906 INFO Environment:547 - hibernate.properties not found
10:51:37,906 INFO Environment:681 - Bytecode provider name : cglib
10:51:37,921 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
10:51:37,984 INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml
10:51:37,984 INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml
10:51:38,031 DEBUG DTDEntityResolver:38 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
10:51:38,031 DEBUG DTDEntityResolver:40 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
10:51:38,031 DEBUG DTDEntityResolver:50 - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
10:51:38,062 DEBUG Configuration:1393 - hibernate.connection.driver_class=org.hsqldb.jdbcDriver
10:51:38,078 DEBUG Configuration:1393 - hibernate.connection.username=sa
10:51:38,078 DEBUG Configuration:1393 - hibernate.dialect=org.hibernate.dialect.HSQLDialect
10:51:38,078 DEBUG Configuration:1393 - current_session_context_class=thread
10:51:38,078 DEBUG Configuration:1393 - cache.provider_class=org.hibernate.cache.NoCacheProvider
10:51:38,078 DEBUG Configuration:1393 - show_sql=true
10:51:38,078 DEBUG Configuration:1393 - hibernate.connection.pool_size=2
10:51:38,078 DEBUG Configuration:1592 - null<-org.dom4j.tree.DefaultAttribute@e4e358 [Attribute: name resource value "org\toto\model\Environment.hbm.xml"]
10:51:38,093 INFO Configuration:559 - Reading mappings from resource : org\toto\model\Environment.hbm.xml