Tuesday, May 11, 2010

Hibernate Tutorial - Fix 4 of 6 - Maven Unable to Find the Hibernate Mapping File

Preface:
I tried to follow the Hibernate Tutorial posted on official Hibernate site. Here is the link to the tutorial. I was using Apache Maven version 2.2.1 and I had to make a lot of modifications to the tutorial code (specially in pom.xml) to make it work. I want to post these fixes/code updates and maybe it might help someone who is having problems walking through the tutorial.

There were so many issues and the post was getting big so I am splitting it into different posts. you can skip reading the 'preface' section of the other posts in this series.

Problem 4 of 6:
Maven unable to find the hibernate mapping file.


I was able to launch HSQL Server. Next step is to add an Event, by executing the EventManager, using the maven command:

mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventFactory" -Dexec.args="store"

And I was getting the following error:
I was getting the following error:

+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'.
[INFO] ------------------------------------------------------------------------
[INFO] Building First hibernate tutorials
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java
[INFO] No goals needed for project - skipping
[WARNING] While downloading javassist:javassist:3.0
This artifact has been relocated to jboss:javassist:3.0.


[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null

resource: org/hibernate/tutorial/domain/Event.hbm.xml not found
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. null
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. null
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:338)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:283)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:31)
at org.hibernate.tutorial.util.HibernateUtil.(HibernateUtil.java:12)
at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:37)
at org.hibernate.tutorial.EventManager.main(EventManager.java:25)
... 6 more
Caused by: org.hibernate.MappingNotFoundException: resource: org/hibernate/tutorial/domain/Event.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1621)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
... 9 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 11 17:47:03 PDT 2010
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------


I moved the hibernate mapping files in to the 'resources' folder, saving the package directory structure.
C:\Users\Krishna\workspace\hibernate\src\main\resources\org\hibernate\tutorial\domain\Event.hbm.xml
Note: C:\Users\Krishna\workspace\hibernate is my project root.

7 comments:

  1. Hi,

    Thanks to you i solved the problem with the pom file !
    But i still having a problem with : org/hibernate/tutorial/domain/Event.hbm.xml not found .

    Untill now , i copy and past the mapping file into the ressource folder , as you indicated it in the "Fix 4 of 6" .
    But it still doesn't work .
    Should I delete the file org/hibernate/tutorial/domain/Event.hbm.xml not found ??

    ReplyDelete
  2. ok , I fix it . I put the Event.hbm.xml (and also Person.hbm.xml), with Event.class
    In the target/classes/org ... directory !

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Tangerine, what Kaizen didn't mention is that you need to run 'mvn compile' first. As part of the build process, that command moves the mapping files from src/main/resources to target/classes.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hey, I made it to problem number 4 before finding this post series. Thanks! Also, sorry for deleted post, the XML did not play nice with the post. I've removed triangle brackets now.

    It seems the reference in hibernate.cfg.xml is wrong also:

    mapping resource="org.hibernate.tutorial.domain/Event.hbm.xml"

    It doesn't turn the dot separated words into a folder structure. I had to change it to:
    mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"

    That's in addition to your change of course.

    ReplyDelete
  7. Great job on this blog post. This tutorial has so many errors. You should volunteer to clean up the tutorial itself.

    ReplyDelete