-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem mapping a table with a declared foreign key
PostPosted: Tue Aug 22, 2006 6:50 pm 
Newbie

Joined: Tue Jun 27, 2006 2:51 pm
Posts: 14
Hi Folks,

I have a table called Trim that contains a TrimId and ModelId column. The TrimId is the PK and ModelId is declared as the FK
Then a separate table called TrimContent has columns such as trimCode, trimPrice etc. I join up the columns in the TrimContent
table to the Trim table using a join tag and all is well. I then create a Model mapping that contains a collection of Trim
objects. As soon as I try to reference the trims, I get the stacktrace below when the application starts up. I've searched through
the forum looking for ways to resolve this, I'm not having much luck. My mappings are below, can anyone spot what I might be
doing wrong? I've tried this with the foreign-key attributes removed and still get the same results. Thanks

Hibernate version:
Hibernate 3

Mapping documents:

Trim mapping:

<hibernate-mapping package="com.mitsubishi.mmsa.domain">

<class name="Trim" table="mmsaBPTrim">

<cache usage="read-write" region="standardCache"/>
<id name="trimId" column="TrimId" type="integer">
<generator class="native"/>
</id>

<property name="modelId" column="ModelId" unique="true"/>

<join table="mmsaBPTrimContent">

<key column="TrimId" foreign-key="ModelId"/>

<property name="trimCode" column="TrimCode" type="string"/>
<property name="trimPrice" column="TrimPrice" type="float"/>
<property name="trimPartnerPrice" column="TrimPartnerPrice" type="float"/>
<property name="trimName" column="TrimNameKey" type="string"/>
<property name="trimTextKey" column="TrimTextKey"/>
<property name="trimFootNote" column="TrimFootNoteKey"/>
<property name="trimImgKey" column="TrimImgKey"/>
<property name="trimDisplaySeq" column="TrimDisplaySeq"/>

</join>

</class>

</hibernate-mapping>

Model Mapping:

<hibernate-mapping package="com.mitsubishi.mmsa.domain">
<class name="Model" table="mmsaBPModel">

<cache usage="read-write" region="standardCache"/>
<id name="modelId" column="ModelId" type="integer">
<generator class="native"/>
</id>

<set name="trims" table="mmsaBPTrim">
<cache usage="read-write" region="standardCache"/>
<key column="ModelId"/>
<many-to-many column="TrimId" foreign-key="ModelId" class="Trim"/>
</set>
...


Full stack trace of any exception that occurs:
org.hibernate.MappingException: Foreign key (ModelId:mmsaBPTrim [TrimId])) must have same number of columns as the referenced primary key (mmsaBPTrim [ModelId,TrimId])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:976)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:825)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:751)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1091)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:396)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:277)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:252)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3729)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4187)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)

Name and version of the database you are using:
SQL Server 2005


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 8:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have set up the table mmsaBPTrim as both a mapped table (for class Trim) and an unmapped join table (for Model.getTrims()). Read up on how to use many-to-many and what the table attribute in the <set> element is used for, as you're using it incorrectly. (refdocs section 6.2.4)

Also, read up on what the foreign-key attribute is for, as you are also using that incorrectly. Unfortunately, this is not documented in a sensible place, you have to look in section 20.1.1.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 9:24 pm 
Newbie

Joined: Tue Jun 27, 2006 2:51 pm
Posts: 14
Hi Tenwit,

Thanks for the reply. I think I'm a little dense here. I've read through the sections you suggested, I tried using the property-ref attribute in the key element within my Model mapping's many-to-many element. I then attempted a bi-directional mapping between my Model and Trim mappings like such:

Model:
Code:

<hibernate-mapping package="com.mitsubishi.mmsa.domain">
   <class name="Model" table="mmsaBPModel">
      
      <cache usage="read-write" region="standardCache"/>
      <id name="modelId" column="ModelId" type="integer">
         <generator class="native"/>
      </id>
      
      <set name="trims" table="mmsaBPTrim">
         <cache usage="read-write" region="standardCache"/>
         <key column="ModelId"/>
         <many-to-many column="TrimId" class="Trim"/>
      </set>

...


Trim:
Code:

<hibernate-mapping package="com.mitsubishi.mmsa.domain">

   <class name="Trim" table="mmsaBPTrim">
   
      <cache usage="read-write" region="standardCache"/>
      <id name="trimId" column="TrimId" type="integer">
         <generator class="native"/>
      </id>
      
      <set name="modelId" table="mmsaBPTrim" inverse="true">
         <cache usage="read-write" region="standardCache"/>
         <key column="TrimId"/>
         <many-to-many column="ModelId" class="Model"/>
      </set>
...


No luck with this either, in fact it gives me the same error message as what I had previously. I seem to be missing something. I took out references to the foreign-key tag, seeing as how that refers to something I'm not even trying to do. Any further suggestions on what I'm doing wrong would help greatly.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 9:55 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The important bit is that you have the wrong value for table in the set element. The table attribute in a collection element (like <set>) names the join table for a many-to-many. This is never either class table.

If you don't have a separate join table, then you don't have a many-to-many relation, you have a one-to-many. I would imagine that this is the case. And seeing as you're using primary keys for association, you probably don't want the property-ref attribute at all.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 11:16 pm 
Newbie

Joined: Tue Jun 27, 2006 2:51 pm
Posts: 14
Awesome,

The one-to-many tip was it. I re-examined the relationships and remapped it. It works great! Thanks so much for the help.


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