-->
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.  [ 2 posts ] 
Author Message
 Post subject: many to many relations with hibernate
PostPosted: Mon Jan 28, 2008 5:43 am 
Newbie

Joined: Mon Jan 28, 2008 1:55 am
Posts: 8
hi all
in my program there are two classes Customer and Account.

in my database i keep two tables customers and accounts to keep customers and Accounts.

one customer can have many accounts and one account can be owned to several customers. so i have many to many relation there.

these are my hbm.xml files
Account.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Account" table="accounts">
<id name="accountNum" column="account_number">
<generator class="assined"/>
</id>
<property name="balance"/>
<property name="branch" />
<set name="customers" table="diposites" lazy="true" inverse="true">
<key>
<column name="account_number" not-null="true"/>
</key>
<many-to-many class="Customer">
<column name="cutomer_id" not-null="true"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>

Customer.hbm.xml


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Customer" table="customers">
<id name="CustomerId" column="customer_id">
<generator class="assigned"/>
</id>
<property name="name" type="String"/>

<set name="accounts" table="diposites" lazy="true" inverse="false">
<key>
<column name="cutomer_id" not-null="false"/>
</key>
<many-to-many class="Account">
<column name="account_number" not-null="true"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
is this the correct way to write hbm.xml file.(i saw it in the net)

but when compile it give this error

compile:
[javac] Compiling 4 source files to /home/wijitha/programing/hibernate/BankExample/build

run:
[echo] Runing the code
[java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[java] log4j:WARN Please initialize the log4j system properly.
[java] Initial SessionFactory creation failed.org.hibernate.MappingException: Error reading resource: dev/Account.hbm.xml
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at util.HibernateUtil.<clinit>(Unknown Source)
[java] at dev.BankManager.createAndStoreCustomer(Unknown Source)
[java] at dev.BankManager.main(Unknown Source)
[java] Caused by: org.hibernate.MappingException: Error reading resource: dev/Account.hbm.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
[java] at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1313)
[java] at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1285)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1267)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1234)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:1162)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:1148)
[java] ... 3 more
[java] Caused by: org.hibernate.MappingException: class Account not found while looking for property: accountNum
[java] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:81)
[java] at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:275)
[java] at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:359)
[java] at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:293)
[java] at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:235)
[java] at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:151)
[java] at org.hibernate.cfg.Configuration.add(Configuration.java:360)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:397)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.java:446)
[java] ... 9 more
[java] Caused by: java.lang.ClassNotFoundException: Account
[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.java:108)
[java] at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:77)
[java] ... 17 more
[java] Java Result: 1

BUILD SUCCESSFUL
Total time: 3 seconds

if any one know the answer please reply me. thanks.

regards
wijitha.


Top
 Profile  
 
 Post subject: Re: many to many relations with hibernate
PostPosted: Mon Jan 28, 2008 7:35 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I think, here is the answer:
wijitha wrote:
hi all
failed.org.hibernate.MappingException: Error reading resource: dev/Account.hbm.xml
[java] Exception in thread "main" java.lang.ExceptionInInitializerError


Hibernate cannot find this expected ressource, and this has nothing to do with many-to-many or the like.

Carlo
----------------------------
please give me credits if this post helped you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.