-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Error reading resource
PostPosted: Sun Jul 17, 2005 2:25 am 
Newbie

Joined: Sun Jul 17, 2005 1:48 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

New in Hibernate checked for similar problems in the forums but I couldn't find anything.

I am following the example on chapter 2 of documentation up to paragraph 2.2. When I try to run the application I am getting MappingException Error reading resource: Event.hbm.xml
What does this means exactly and how do I fix it;

I have created the database and user on MySQL and checked that the access rights are ok.


Hibernate version: 3.0.5

Mapping documents:The Event.hbm.xml is according to the documentation as well as the java files Event.Java, EventManager.java and HibernateUtil.java

<?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="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: Error reading resource: Event.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.nn.Event.HibernateUtil.<clinit>(HibernateUtil.java:30)
at com.nn.Event.EventManager.createAndStoreEvent(EventManager.java:40)
at com.nn.Event.EventManager.main(EventManager.java:26)
Caused by: org.hibernate.MappingException: Error reading resource: Event.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:452)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at com.nn.Event.HibernateUtil.<clinit>(HibernateUtil.java:26)
... 2 more
C
Name and version of the database you are using: MySQL 4.1.12-max

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 2:44 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
I think it's having trouble finding the Event.hbm.xml file. Depending on how you've written your hibernate config file, you probably need to put Event.hbm.xml in the package with Event.java. If that doesn't do it for you, post your hibernate config file.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 5:58 am 
Newbie

Joined: Sun Jul 17, 2005 1:48 am
Posts: 4
eagle79 wrote:
I think it's having trouble finding the Event.hbm.xml file. Depending on how you've written your hibernate config file, you probably need to put Event.hbm.xml in the package with Event.java. If that doesn't do it for you, post your hibernate config file.


No that's not the problem. That's the message when not finding the file
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: Resource: Event2.hbm.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError


The hibernate config file according to chapter 2 of the documentation
<?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/event</property>
<property name="connection.username">event</property>
<property name="connection.password">event</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>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<mapping resource="Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 11:02 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
If you have Event.hbm.xml in the package with Event, remove this line from your config.

Code:
<mapping resource="Event.hbm.xml"/>


Also (and I think this is the problem), you need to specify the package for Event somewhere in the mapping. So, if your event is in package.name, either:

Code:
<hibernate-mapping package="package.name">
     <class name="Event" table="EVENTS">
...

or
Code:
<hibernate-mapping>
     <class name="package.name.Event" table="EVENTS">
...


If Event isn't in a package, I'm not sure what would be correct, except to say that you should put it in one ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 12:40 pm 
Newbie

Joined: Sun Jul 17, 2005 1:48 am
Posts: 4
eagle79 wrote:
Also (and I think this is the problem), you need to specify the package for Event somewhere in the mapping. So, if your event is in package.name, either:
Code:
<hibernate-mapping package="package.name">
     <class name="Event" table="EVENTS">

or
Code:
<hibernate-mapping>
     <class name="package.name.Event" table="EVENTS">



Thanks that solved the problem. I tried both
Code:
<hibernate-mapping package="package.name">

and
Code:
<class name="package.name.Event" table="EVENTS">


and both solve the problem.

I have the Event.hbm.xml on the root of the src directory and it works fine.
I thought it will be better to have the mapping files together with the java classes but then I had to modify the hibernate.cfg.xml file to define the path of the mapping file in the package.
Code:
<mapping resource="com/xxxx/xxxx/Event.hbm.xml"/>


Is there any standard praxis regarding the location of the mapping files;


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 3:42 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Quote:
I thought it will be better to have the mapping files together with the java classes but then I had to modify the hibernate.cfg.xml file to define the path of the mapping file in the package.

Is there any standard praxis regarding the location of the mapping files;


The preferred practice is to place an hbm.xml file for each persistent class in the package with the .java file. If you do this, then you need specify nothing in your hibernate.cfg.xml file to "import" the config. Instead, you simply add the class to your Configuration object before building the SessionFactory:

Code:
sessionFactory = new Configuration()
                         .addClass(Event.class)
                         .configure()
                         .buildSessionFactory();


Hibernate will assume you have followed preferred practice.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.