-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-many bidirectional association query
PostPosted: Wed Sep 29, 2010 1:17 pm 
Newbie

Joined: Wed Sep 29, 2010 12:58 pm
Posts: 5
Hi Folks,

I have been struggling with this problem of creating a one-to-many bidirectional association on a join table.
I have a scenario where one DEVICE can be associated with many NODE's and the join table I use is DEVICE_NODE.

I am using hibernate tools to generate the code automatically and create the schema. On disabling the join in Device.hbm.xml, things work perfect and code is generated as per unidirectional one-to-many mapping with the creation of the DEVICE_NODE join table.
However if I leave the join portion uncommented(followed this example - http://docs.jboss.org/hibernate/core/3. ... l-join-12m), things fail with an exception which says -

org.hibernate.InvalidMappingException: Could not parse mapping document from resource Device.hbm.xml
Could not parse mapping document from resource Device.hbm.xml
org.hibernate.PropertyNotFoundException: field [node] not found on Device
field [node] not found on Device

Can someone let me know if am missing anything ?

Cheers n thanks
~v

Here are the hbm.xmls.

Device.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 29 Sep, 2010 10:54:52 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="Device" table="DEVICE" schema="PUBLIC" catalog="H2">
<id name="devId" type="java.lang.Long">
<column name="DEV_ID" />
<generator class="identity"></generator>
</id>
<property name="devType" type="string">
<column name="DEV_TYPE" length="30" not-null="true" />
</property>
<property name="desc" type="string">
<column name="DESC" length="30" />
</property>
<!-- bidirectional many-to-one - Always fails with the exception when uncommented >
<join table="DEVICE_NODE" inverse="true" optional="true">
<key column="DEV_ID"/>
<many-to-one name="node"
column="NODE_ID"
not-null="true"/>
</join-->

</class>
</hibernate-mapping>

Node.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 29 Sep, 2010 9:25:12 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="Node" table="NODE" schema="PUBLIC" catalog="H2">
<id name="nodeId" type="java.lang.Long">
<column name="NODE_ID" />
<generator class="identity"></generator>
</id>
<property name="desc" type="string">
<column name="DESC" length="30" />
</property>
<property name="location" type="string">
<column name="LOCATION" length="30" />
</property>
<set name="devices" table="DEVICE_NODE" inverse="false" lazy="true">
<key>
<column name="NODE_ID" not-null="true" />
</key>
<many-to-many entity-name="Node">
<column name="DEV_ID" not-null="true" unique="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Wed Sep 29, 2010 2:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Does your Device class have getNode()/setNode() methods?


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Wed Sep 29, 2010 10:13 pm 
Newbie

Joined: Wed Sep 29, 2010 12:58 pm
Posts: 5
Am trying to generate my classes from the hbm.xmls. So I dont have the classes yet.

thanks!


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Thu Sep 30, 2010 2:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need classes that matches the hbm.xml files. Otherwise Hibernate will not work.


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Thu Sep 30, 2010 2:08 am 
Newbie

Joined: Wed Sep 29, 2010 12:58 pm
Posts: 5
Well, I am trying to generate my entity classes and schema ddl from the hbm.xmls using hibernate tools inside eclipse.
Once I get my classes generated(with annotations), I will throw away the xmls as they wont be needed anymore.

I havent got as far as running my hibernate program yet.

cheers!


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Thu Sep 30, 2010 2:15 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Ok, I didn't notice that your question was about Hibernate Tools. Maybe you should post it in the Tools forum: viewforum.php?f=6


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Thu Sep 30, 2010 3:28 am 
Newbie

Joined: Wed Sep 29, 2010 12:58 pm
Posts: 5
Sorry bout that, I shud have posted it in the right forum.

I have resolved this issue by eliminating the use of hbm.xml totally. I have used annotations and the bidirectional with join table works perfectly.

Here r the relevant portions of the entity class :

Device.java
>>>>
@ManyToOne(optional=true)
@JoinTable(name = "NODE_DEVICE",
joinColumns = {
@JoinColumn(name="DEV_ID")
},
inverseJoinColumns = {
@JoinColumn(name="NODE_ID")
}
)
public Node getNode() {
return node;
}
<<<<

Node.java
>>>>>
@OneToMany
@JoinTable(name = "NODE_DEVICE",
joinColumns = {
@JoinColumn(name="NODE_ID", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="DEV_ID")
}
)
public Set<Device> getDevices() {
return this.devices;
}
<<<<<

thnx again
Cheers!
~v


Top
 Profile  
 
 Post subject: Re: one-to-many bidirectional association query
PostPosted: Thu Sep 30, 2010 3:37 am 
Newbie

Joined: Wed Sep 29, 2010 12:58 pm
Posts: 5
But the question now is, how will hibernate automatically add a tuple in the mapping table.
if I do a save(device) and save(node), will the row be automatically inserted in join table?

cheers!
~v


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.