Hi All,
I am having trouble mapping 2 tables via a link table. I am a newbie to this, so please be gentle -- and basic.
I have 3 tables:
SITE_REF, where the PK is site_id
NETWORK_REF, where the PK is network_id
and SITE_NETWORK_REF, where the PK and FK are site_id and network_id
As far as cardinality, I came up with many-to-one… as a Site can have more than one networkID, but a networkID cannot belong to more than one Site.
I created domain objects for all three tables, with xml mappings for each.
SiteNetworkRef.hbm.xml which maps to SITE_NETWORK_REF:
Code:
<class name="domain.SiteNetworkRef"
table="site_network_ref" proxy="domain.SiteNetworkRef"
dynamic-update="true" dynamic-insert="true">
<composite-id>
<key-property name="siteID" column="site_id" />
<key-property name="networkID" column="network_id" />
</composite-id>
</class>
Site.xml which maps to SITE_REF:
Code:
<class name="domain.Site" table="site_ref"
proxy="domain.Site" dynamic-update="true"
dynamic-insert="true">
<id name="id" column="site_id" type="string"></id>
<property name="siteType" type="string" column="site_type" />
<property name="wirelessIndicator" type="char"
column="wireless_ind" />
<property name="dialupIndicator" type="char"
column="dialup_ind" />
<property name="expirationDate" column="expiration_dtm"
type="date" update="true" insert="true" />
</class>
NetworkData.xml which maps to NETWORK_REF:
Code:
<class name="domain.NetworkData" table="network_ref"
proxy="domain.NetworkData" dynamic-update="true"
dynamic-insert="true">
<id name="id" column="network_id" type="int"></id>
<property name="subnetIP" type="string" column="subnet_ip" />
<property name="subnetMask" type="string" column="subnet_mask" />
<property name="expirationDate" column="expiration_dtm"
type="date" update="true" insert="true" />
<join table="site_network_ref" inverse="false"
optional="false">
<key column="site_id" />
<many-to-one name="networkID"
class="domain.SiteNetworkRef" column="network_id"
not-null="true" />
</join>
</class>
My web service will be given a siteID, that will have to be looked up in the SITE_NETWORK_REF table, get the associated networkID’s, and then look in the NETWORK_REF table for the data desired. To do this, I put a join attribute in my NetworkData.hbm.xml file – but this is what I am unsure if I am doing correctly. If I leave the files as they are now, I will get this hibernate exception:
Code:
2924 [main] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/MAP] - Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Foreign key (FK8FC060ABD811DF0:site_network_ref [network_id])) must have same number of columns as the referenced primary key (site_network_ref [site_id,network_id])
org.hibernate.MappingException: Foreign key (FK8FC060ABD811DF0:site_network_ref [network_id])) must have same number of columns as the referenced primary key (site_network_ref [site_id,network_id])
Which honestly I don’t fully understand what it means, but I know its because I have only one id in my NetworkData.hbm.xml, and 2 in the SiteNeworkRef.hbm.xml that I am referencing. So to get around that, I changed my SiteNetworkRef mapping to:
Code:
<class name="domain.SiteNetworkRef"
table="site_network_ref" proxy="domain.SiteNetworkRef"
dynamic-update="true" dynamic-insert="true">
<id name="id" column="site_id" type="string"></id>
<property name="networkID" type="int" column="network_id" />
</class>
Hibernate executes the query like this:
Code:
Hibernate: select networkdat0_.network_id as network1_2_, networkdat0_.subnet_ip as subnet2_2_, networkdat0_.subnet_mask as subnet3_2_, networkdat0_.expiration_dtm as expiration4_2_, networkdat0_1_.network_id as network2_3_ from network_ref networkdat0_ inner join site_network_ref networkdat0_1_ on networkdat0_.network_id=networkdat0_1_.site_id where last_uptd_dtm>=? and site_id=?
Where it is joining the NETWORK_REF and SITE_NETWORK_REF table where the networkID == siteID, which will never happen! No matter what I do, I can’t seem to get it to want to join where the siteID given to the web service == the siteID stored in the SITE_NEWORK_REF table.
Can anyone please offer some guidance? Thanks so much in advance!