Hibernate version:
3.1.3
Mapping document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.bps.iproject.domain.audit.AuditEntry"
table="AuditTrail"
optimistic-lock="none"
>
<id
name="id"
column="AuditTrailId"
type="long"
unsaved-value="-1"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-AuditEntry.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="created"
type="timestamp"
update="false"
insert="true"
column="Created"
/>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="Username"
length="30"
not-null="true"
/>
<property
name="actionId"
type="int"
update="true"
insert="true"
column="ActionId"
not-null="true"
/>
<component
name="objectPointer"
class="com.bps.iproject.domain.DomainObjectPointer"
>
<property
name="objectId"
type="long"
update="true"
insert="true"
column="ObjectId"
not-null="true"
/>
<property
name="objectType"
type="int"
update="true"
insert="true"
column="ObjectType"
not-null="true"
/>
</component>
<property
name="message"
type="java.lang.String"
update="true"
insert="true"
column="Message"
length="255"
/>
<property
name="messageParams"
type="java.lang.String"
update="true"
insert="true"
column="MessageParms"
length="255"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-AuditEntry.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code:
This error is occurring when I’m configuring my SessionFactory. The relevant code snippet is:
File classesDirectory = new File("./build/classes");
Configuration hibernateConf = new Configuration();
hibernateConf = hibernateConf.addDirectory(classesDirectory);
SessionFactory sessionFactory = hibernateConf.buildSessionFactory();
Stack Trace:
org.hibernate.MappingException: Could not read mapping document from file: .\build\classes\com\bps\iproject\domain\audit\AuditEntry.hbm.xml
at org.hibernate.cfg.Configuration.addFile(Configuration.java:270)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:583)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:580)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:580)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:580)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:580)
at org.hibernate.cfg.Configuration.addDirectory(Configuration.java:580)
at com.bps.iproject.test.BaseTestCaseHibernate.createSessionFactory(BaseTestCaseHibernate.java:146)
at com.bps.iproject.test.BaseTestCaseHibernate.<clinit>(BaseTestCaseHibernate.java:71)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at junit.framework.TestSuite.createTest(TestSuite.java:131)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:114)
at junit.framework.TestSuite.<init>(TestSuite.java:75)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest(RemoteTestRunner.java:399)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
at org.hibernate.cfg.Configuration.addFile(Configuration.java:267)
... 19 more
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:353)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
... 20 more
Database:
MySQL 4.1
Description:
This is an error I was getting when running a JUnit test from within Eclipse.
After much traipsing through the XML parser’s code I was able to isolate the line of code that was causing the NPE. It’s inside org.hibernate.util.DTDEntityResolver:
protected InputStream resolveInHibernateNamespace(String path) {
return this.getClass().getClassLoader().getResourceAsStream( path );
}
It turns out the problem was that I had the hibernate jar file being loaded by Eclipse as one of the Bootstrap Entries (duh) and the call to getclassLoader() was returning null (as is its prerogative when loaded by the bootstrap class loader), hence the NPE when it tried to then call getResourceAsStream(...).
The obvious fix for me to get my unit test working was to move the Hibernate jar down into the User Entries and the problem goes away after that. But in the course of diagnosing this problem I was googling around and found this bug report
http://opensource.atlassian.com/project ... se/HB-1370
which suggested that this bug had been fixed in hibernate versions 2.1.8 and 3.0 beta 2. The fix purposed by that bug was to change the code to something like
// Search for DTD
ClassLoader classLoader = this.getClass().getClassLoader();
// The class may be loaded by the bootstrap class loader
InputStream dtdStream = (classLoader == null) ?
this.getClass().getResourceAsStream( path) :
classLoader.getResourceAsStream(path);
But it doesn’t look like this fix made it into 3.1.3. Am I missing something or did this bug fix legitimately get lost in the shuffle between 3.0 beta 2 and 3.1.3?