Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate + Configuration.addClass Exception

 
Steve Press
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

I have a following problem with Hibernate application which I develope in
Eclipse environment.

Application is a simple Java standalone program and it has
hibernate.cfg.xml file and a class for mapping a class to DB.

Hibernate.cfg.xml file is read without problems but
everytime in code when Configure class is invoking addClass-method
MappingException is thrown.

E.g.call
cfg.addClass(Product.class);
throws

08-Oct-2004 10:57:22 net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: hibertest/Product.hbm.xml
Exception in thread "main" net.sf.hibernate.MappingException: Resource: hibertest/Product.hbm.xml not found
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:352)
at hibertest.HibTestMain.main(HibTestMain.java:38)

I have strugled with this 2 and half days

Where does Hibernate actually reads XML mapping files?

I have put all my XML files to same place which is in the root of
Eclipse project so, if hibernate.cfg.xml is found should'n mapping XMLs found too?

I have following hibernate.cfg.xml file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc ostgresql://localhost/TesterDB
</property>
<property name="show_sql">false</property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.connection.username">usepostg</property>
<property name="hibernate.connection.password" />
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- Mapping files -->
<mapping resource="Product.hbm.xml" />
</session-factory>
</hibernate-configuration>

and Product.hbm.xml file for class OR mapping is:

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="hibertest.Product" table="Product">

<id name="id" type="string"
unsaved-value="null">
<column name="id" sql-type="varchar(32)"
not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" sql-type="varchar(32)"
not-null="true"/>
</property>
<property name="price" type="java.lang.Double">
<column name="price" sql-type="int8"
not-null="true"/>
</property>
<property name="amount" type="java.lang.Long">
<column name="amount" sql-type="int4"
not-null="true"/>
</property>
</class>
</hibernate-mapping>
 
bamini mini
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi



when you give the name of your column in hbm file, it must be different from the column name.

you dont give the same name for property name and column name.

for eg.

<property name="name" column="studentName"/>

both column nameand property name mustnot be same.

ok
and when you are writting the jave file for getting property

u call the getterfile and setterfile like this.
public string getName()
{
return name;}
public void setName(String value)
{
this.name=value;
}

ok change the property name in hbm file.you should give different name from the column name

bye
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when you give the name of your column in hbm file, it must be different from the column name.

Hmm. Not sure why you think this, but its not the case. There is nothing to stop you giving property names the same values as DB field names.

Try putting product.hbm.xml in the same place as your product.class file (which I assume is in the directory "hibertest").
[ October 11, 2004: Message edited by: Paul Sturrock ]
 
Madhu Kumar Vatts
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey steve,

I got similar problem when I was trying to debug my application using Eclipse. It was throwing the mapping file not found exception. I solved it by placing the files in the class path. hope it helps you.

Let me know if you have questions or if this helped you

Thanks
Madhu
 
Madhu Kumar Vatts
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

I thought I will give you some more information which might help you in solving your problem
I will describe little how my application layers are divided and how hibernate loads the hibernate.cfg.xml file and corresponding mapping files.

I have these directories
1. cfg -> where all the mapping files are present(hibernate.cfg.xml, *.hbm.xml)
2. lib
3. src -> Main source code from which mapping files are called
4. bin

This is the way I call the mapping files
----------------------------------------------------------
CODE
----------------------------------------------------------
import net.sf.hibernate.cfg.Configuration;
private static final Configuration cfg = new Configuration();
cfg.configure("/hibernate.cfg.xml");
----------------------------------------------------------
----------------------------------------------------------
this loads all the necessary mapping files necessary for me.

Let me know if this helps

Thanks,
Madhu
 
Peter Boyle
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having weird problem. My Person.hbm.xml is next to hibernate.cfg.xml, and upon loading hibernate.cfg.xml (which has reference to Person.hbm.xml), I get the exception that Person.hbm.xml has not found.

Has anyone know the solution to this problem? I am using Tomcat 3.3, does that matter?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic