aspose file tools*
The moose likes Object Relational Mapping and the fly likes could not read mapping from resource 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
Reply locked New topic
Author

could not read mapping from resource

Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
Hi guys,

Im fairly new to Hibernate so will not be surprised if this problem is the result of something stupid I have done, and hopefully really easy to fix.

I have two tables Routine and Skill, and Routine has a List of skills. When I try to get the current session, it maps Routine fine and then stops at Skill, saying it could not read mapping from resource: Skill.hbm.xml.

If you want me to post any code let me know.
Any suggestions or help would be greatly appreciated.

Thanks Kazza
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Is Skill.hbm.xml on the classpath? Is it configured in hibernate.cfg.xml?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
Originally posted by Paul Sturrock:
Is Skill.hbm.xml on the classpath? Is it configured in hibernate.cfg.xml?


Yeah its in the same path as the Routine.hbm.xml and the java files so has to be on classpath. By configured in the hibernate.cfg.xml do you jut mean the file is added at the bottom, like:
<mapping resource="Routine.hbm.xml"/>
<mapping resource="Skill.hbm.xml"/>
or is there something else I need to add?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hmm. If both these things are true you won't see this error. Best to double check I think. Look for spelling mistakes in your mapping files, make sure the hbm.xml files are in the packages you expect them to be and they are packaged with your classes correctly.

Also remember, your SessionFactory must be configured somewhere. If you are using hibernate.cfg.xml to do this every mapping file must by included. If your mapping files (and classes) are in the default package the two lines your posted should be fine.
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
Originally posted by Paul Sturrock:
Hmm. If both these things are true you won't see this error. Best to double check I think. Look for spelling mistakes in your mapping files, make sure the hbm.xml files are in the packages you expect them to be and they are packaged with your classes correctly.

Also remember, your SessionFactory must be configured somewhere. If you are using hibernate.cfg.xml to do this every mapping file must by included. If your mapping files (and classes) are in the default package the two lines your posted should be fine.


I have checked and checked for spelling mistakes or any little extra things. Im configuring it with the hibernate.cfg.xml and all mapping files are included. Could it be anything to do with this:
<list name="skills" cascade="all-delete-orphans">
<key column="routine_id" not-null="true"/>
<list-index column="skill_index"/>
<one-to-many class="Skill"/>
</list>
This is in the Routine.hbm.xml to represent my list of skills. Do I need anything in Skill.hbm to match this. I want it to be unidirectional.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Do I need anything in Skill.hbm to match this. I want it to be unidirectional.

If your relationship is unidirectional, then no.

One thing I notice is all your mappings so far don't include any package information. Are all your mapping files in the default package?
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
Yeah, I have only the default package and then seperate folders inside this. I got started on the project before I really understood the IDE enough to think about packages.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hmm. You are running this in an IDE? Is your IDE configured to build hbm.xml files to the same directory as your class files?

There is one other thing that could cause this I believe. You might want to check your DOCTYPE declaration: is it pointing at the right DTD (for the version of Hibernate you are using) and is that DTD accessable?
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
The DTD and all that top stuff is exactly the same in Skill as it is in Routine.
Plus the Routine.hbm.xml is in the same folder as Skill.hbm.xml and they are both being added to the bin folder with the class files when project is built. Routine is being mapped without a problem.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its got to be a typo then. Can you write code to access Skill directly (i.e. not as part of the list)?
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
Not sure I understand what you mean. Skill is just a normal object, it is only used by the Routine list. A skill object gets created and then added to Routine's list.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I mean, can you do this:

where a Skill exists in your database with an ID of 1.
Kazza Summers
Greenhorn

Joined: May 30, 2006
Posts: 8
This is the line of code it cant get past:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

so I cant even get to the part where i can load the object.
Not sure if this will help but here is the HibernateUtil class:
public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}


It is screwing up on the try catch block and throwin the exception.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ok. So now you know that it is your SessionFactory configuration that is to blame. What Hibernate does when you call configure().buildSessionFactory() is steps through all the hbm.xml mapping files you have defined in your configuration file (hibernate.cfg.xml), tries to parse the mapping then "bind" the class named in that mapping. I've already mentioned the two likely (and one less likely) reasons why this could be failing. I can't offer you any more advice than checking these things very carefully.
Manju Viswam
Greenhorn

Joined: Jun 11, 2006
Posts: 2
Dear Kazza,
Were u able to solve your problem ?
I am getting the same error as yours.
I re-checked everything as Mr.Paul said.But still the same error.See the trace below:
-------------------------------------------------------------------------
INFO: Reading mappings from resource: Message.hbm.xml
Jun 11, 2006 12:41:29 PM org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(5) Element type "hibernate-mapping"
s not declared.
org.hibernate.MappingException: Could not read mappings from resource: Message.h
bm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.jav
a:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.jav
a:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
-------------------------------------------------------------------------


Manju Viswam
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Manju Viswam, please don't hijack someone elses topic. The error you have is different:


SEVERE: Error parsing XML: XML InputStream(5) Element type "hibernate-mapping"
s not declared

You seem to have an invalid hbm.xml file. Check Message.hbm.xml against the DTD. Are all the mandatory elements there (in particular hibernate-mapping?)
Manju Viswam
Greenhorn

Joined: Jun 11, 2006
Posts: 2
Dear Mr. Paul,

You were exactly right.I had given the DTD declaration of the cfg.xml file inside hbm.xml. I corrected the DTD of hbm.xml to
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Its working fine now.
Thank you so much !
Alongside,this new javarancher, apologise for replying to Mr.Kazza's post.I thought this is a conversation thread

Thanks & cheers,
Manju Viswam
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Alongside,this new javarancher, apologise for replying to Mr.Kazza's post.I thought this is a conversation thread

No need to apologise . We prefer one problem to one topic thats all; makes it much easier for others to follow. Glad you resolved your problem.
kumari Jain
Ranch Hand

Joined: Jul 28, 2004
Posts: 44
hi Kazza Summers,
i encountered the same problem as yours.But even though this exception is displayed,if you look at the the weblogic prompt,few lines below this exception there is another exception being thrown basically NoClassDefFoundError,this could be that hibernate3.jar is not accessible.

regards,
kumari
Rich Youngkin
Greenhorn

Joined: Jan 17, 2007
Posts: 2
Originally posted by kumari Jain:
...if you look ...few lines below this exception there is another exception being thrown basically NoClassDefFoundError,this could be that hibernate3.jar is not accessible.

regards,
kumari


Another cause of the NoClassDefFoundError/ClassNotFoundException is that the class that is referenced by the <class name=Foo ...> attribute in the hbm file can't be found in the classpath. The class name could be misspelled (e.g., foo vs. Foo) or missing altogether (which was the cause in my case).

Cheers,
Rich
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Rich Youngkin:


Another cause of the NoClassDefFoundError/ClassNotFoundException is that the class that is referenced by the <class name=Foo ...> attribute in the hbm file can't be found in the classpath. The class name could be misspelled (e.g., foo vs. Foo) or missing altogether (which was the cause in my case).

Cheers,
Rich


Hi Rich, please note the dates on the original thread, they are about 8 months old.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ilya Kogan
Greenhorn

Joined: Aug 04, 2007
Posts: 1
In case anyone else get this error:
I got this error and the reason was that a property name that existed in the hbm.xml file was missing as a field in the java file.
Good luck
Facundo Chamb´┐Ż
Greenhorn

Joined: May 08, 2008
Posts: 1
I was getting this error because the mappings in hibernate.cfg.xml were in the wrong order.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I am going to close this resurrected thread. This thread is two years old.

Thanks

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could not read mapping from resource
 
Similar Threads
Can I go to take the exam now?
SAROS document manager API
retrieve values from database at the time of form loading using struts
problem in mapping hibernate class
problems during presentation