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>