This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes InvalidMappingException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "InvalidMappingException" Watch "InvalidMappingException" New topic
Author

InvalidMappingException

Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Hello everyone. I'm trying to run my first Hibernate 3.0 program. I've searched the world of google but I'm still stuck. Any assistance is greatly appreciated! My apologies for the length.

+----------------------------------------+
| Directory Structure: Before running ANT
+----------------------------------------+

hib
+lib
+src
+com
+cundiff
+claude
+library
Book.hbm.xml
Book.java
DoIt.java
hibernate.cfg.xml
build.xml

+----------------------------------------+
| StackTrace
+----------------------------------------+

copy-resources:
[copy] Copying 2 files to c:\Java_Programs\hib\bin

compile:
[javac] Compiling 2 source files to c:\Java_Programs\hib\bin

execute:
[echo] Hi
[java] Got past try
[java] log4j:WARN No appenders could be found for logger (org.hibernate.cf
.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] 1
[java] Exception in thread "main" org.hibernate.InvalidMappingException: C
uld not parse mapping document from resource com/cundiff/claude/library/Book.hb
.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.ja
a:602)
[java] at org.hibernate.cfg.Configuration.parseMappingElement(Configur
tion.java:1621)
[java] at org.hibernate.cfg.Configuration.parseSessionFactory(Configur
tion.java:1589)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.ja
a:1568)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.ja
a:1542)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java
1462)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java
1448)
[java] at com.cundiff.claude.library.DoIt.main(Unknown Source)
[java] Caused by: org.hibernate.InvalidMappingException: Could not parse m
pping document from invalid mapping
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration
java:535)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.ja
a:599)
[java] ... 7 more
[java] Caused by: org.xml.sax.SAXParseException: Element type "session-fac
ory" must be declared.
[java] at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.
reateSAXParseException(ErrorHandlerWrapper.java:195)
[java] at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.
rror(ErrorHandlerWrapper.java:131)
[java] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.rep
rtError(XMLErrorReporter.java:384)
[java] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.rep
rtError(XMLErrorReporter.java:318)
[java] at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.
andleStartElement(XMLDTDValidator.java:1929)
[java] at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.
tartElement(XMLDTDValidator.java:764)
+----------------------------------------+
| Book.java
+----------------------------------------+

+----------------------------------------+
| Book.hbm.xml
+----------------------------------------+
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>

<session-factory>

<class name="Book" table="Book">

<id name="id" column="id">

<generator class="increment"/>

</id>

<property name="title"/>

<property name="isbn"/>

</class>

</session-factory>

</hibernate-mapping>
+----------------------------------------+
| DoIt.java
+----------------------------------------+

+----------------------------------------+
| hibernate.cfg.xml
+----------------------------------------+
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost/hib</property>

<property name="connection.username">root</property>

<property name="connection.password">somepassword</property>

<!-- JDBC connection pool (use the built-in) -->

<property name="connection.pool_size">1</property>

<!-- SQL dialect -->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management-->

<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->

<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->

<property name="hbm2ddl.auto">create</property>

<mapping resource="com/cundiff/claude/library/Book.hbm.xml"/>

</session-factory>

</hibernate-configuration>


+----------------------------------------+
| ANT Build.xml
+----------------------------------------+
<project name="hibernate-tutorial" default="execute">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>

<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>

<!--Classpath declaration-->
<path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
</path>

<!--Clean up-->
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>

<!--Compile-->
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>

<!--Copy-->
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>

<!--Run Program-->
<target name="execute" depends="compile">
<echo>Hi</echo>
<java fork="true"
classname="com.cundiff.claude.library.DoIt"
classpathref="libraries">
<classpath path="${targetdir}"/>
</java>

</target>

</project>
+----------------------------------------+
| mySql Databse hib.sql
+----------------------------------------+
#create database
CREATE DATABASE hib;

#select this db for use
USE hib;

#Create Book table
CREATE TABLE book
(
id SMALLINT(6) primary key auto_increment,
isbn VARCHAR(25) not null,
title VARCHAR(50) not null
);
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
My apologies, here is a better directory structure.

+----------------------------------------+
| Directory Structure: Before running ANT
+----------------------------------------+

hib
---+lib
---+src
------+com
---------+cundiff
------------+claude
---------------+library
------------------Book.hbm.xml
------------------Book.java
------------------DoIt.java
------hibernate.cfg.xml
---build.xml
aswin kumar
Greenhorn

Joined: Dec 25, 2008
Posts: 12
hi,

remove <session-factory> tag and </session-factory> from hbm.xml file,and check it,

regards,
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Good catch!

However, I'm still getting the same stack trace
aswin kumar
Greenhorn

Joined: Dec 25, 2008
Posts: 12
hi,
have you saved and executed again? the same error will not repeat .

regards
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
Here is the stack trace after making the suggested adjustments:


c:\Java_Programs\hib>ant
Buildfile: build.xml

clean:
[delete] Deleting directory c:\Java_Programs\hib\bin
[mkdir] Created dir: c:\Java_Programs\hib\bin

copy-resources:
[copy] Copying 2 files to c:\Java_Programs\hib\bin

compile:
[javac] Compiling 2 source files to c:\Java_Programs\hib\bin

execute:
[echo] Hi
[java] Got past try
[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg
.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] 1
[java] Exception in thread "main" org.hibernate.InvalidMappingException: Co
uld not parse mapping document from resource com/cundiff/claude/library/Book.hbm
.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:602)
[java] at org.hibernate.cfg.Configuration.parseMappingElement(Configura
tion.java:1621)
[java] at org.hibernate.cfg.Configuration.parseSessionFactory(Configura
tion.java:1589)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1568)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1542)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1462)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1448)
[java] at com.cundiff.claude.library.DoIt.main(Unknown Source)
[java] Caused by: org.hibernate.MappingException: class Book not found whil
e looking for property: id
[java] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:97)
[java] at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(Simp
leValue.java:302)
[java] at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:423)
[java] at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValu
es(HbmBinder.java:356)
[java] at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:295)

[java] at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:166)
[java] at org.hibernate.cfg.Configuration.add(Configuration.java:702)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.
java:537)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:599)
[java] ... 7 more
[java] Caused by: java.lang.ClassNotFoundException: Book
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

[java] at java.lang.Class.forName0(Native Method)
[java] at java.lang.Class.forName(Class.java:169)
[java] at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.j
ava:123)
[java] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(Refle
ctHelper.java:93)
[java] ... 15 more
[java] Java Result: 1

BUILD SUCCESSFUL
Total time: 3 seconds
c:\Java_Programs\hib>
==========================================================================
I'm wondering if I have all the .jar files that I need... Here my directory:

Directory of c:\Java_Programs\hib\lib

12/28/2008 05:22 PM <DIR> .
12/28/2008 05:22 PM <DIR> ..
08/20/2008 11:19 AM 194,050 ant-contrib-1.0b2.jar
08/20/2008 11:19 AM 74,237 ant-junit-1.6.5.jar
06/13/2008 12:09 PM 443,432 antlr-2.7.6.jar
08/20/2008 11:20 AM 443,432 antlr.jar
08/20/2008 11:20 AM 16,777 asm-attrs.jar
08/20/2008 11:20 AM 26,360 asm.jar
12/27/2008 03:52 PM <DIR> build
08/20/2008 11:20 AM 282,338 cglib.jar
06/13/2008 12:09 PM 559,366 commons-collections-3.1.jar
08/20/2008 11:20 AM 559,366 commons-collections.jar
06/13/2008 12:09 PM 313,898 dom4j-1.6.1.jar
08/20/2008 11:20 AM 313,898 dom4j.jar
08/20/2008 11:20 AM 50,583 ejb3-persistence.jar
08/20/2008 11:20 AM 279,714 hibernate-annotations.jar
08/20/2008 11:20 AM 66,993 hibernate-commons-annotations.jar
08/20/2008 11:20 AM 2,266,769 hibernate-core.jar
06/13/2008 12:10 PM 471,005 javassist-3.4.GA.jar
08/20/2008 11:20 AM 471,005 javassist.jar
06/13/2008 12:09 PM 13,236 jta-1.1.jar
08/20/2008 11:20 AM 13,236 jta.jar
08/20/2008 11:19 AM 121,070 junit-3.8.1.jar
08/20/2008 11:20 AM 121,070 junit.jar
08/20/2008 11:20 AM 367,444 log4j.jar
08/20/2008 11:19 AM 425 README.txt
08/19/2008 07:40 PM 17,384 slf4j-api-1.5.2.jar
08/20/2008 11:20 AM 15,279 slf4j-api.jar
08/20/2008 11:20 AM 8,620 slf4j-log4j12.jar
12/27/2008 03:52 PM <DIR> test
26 File(s) 7,510,987 bytes
4 Dir(s) 56,312,975,360 bytes free

Again, I really appreciate the help!
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
I figured the InvalidMappingException problem. It was in the book.hbm.xml. Here is the new version.

Now it throws a HibernateException (JDBC driver not found)

...slowly getting there.

<?xml version="1.0"?>
<!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.cundiff.claude.library.Book" table="book">

<id name="id" type="long" column="id">

<generator class="increment"/>

</id>

<property name="title"/>

<property name="isbn"/>

</class>

</hibernate-mapping>
aswin kumar
Greenhorn

Joined: Dec 25, 2008
Posts: 12
Hi,

just check whether you added mysql driver jar file in your classpath,

regards,
Claude Cundiff
Ranch Hand

Joined: Mar 20, 2008
Posts: 78
I added the driver and got it up and running. Had to work the DoIt class a bit but finally!

Happy New Year!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InvalidMappingException
 
Similar Threads
NoClassDefFoundError
Ant - Directory not created
Could not parse configuration: /hibernate.cfg.xml
Problem with Ant Build
Data is not inserting