Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Foreign key must have same number of columns as the referenced primary key

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following 3 tables in my DB:

Distributionlist
distributionlistid (Primary Key)

Serviceuser
serviceuserid (Primary Key)

Listmember
distributionlistid (Foreign key to Distributionlist)
serviceuserid (Foreign key to Serviceuser)

Primary key is a composite of distributionlistid and serviceuserid



(basically Listmember is an assoc table that allows me to map Listmembers and Distributionlists together)




I also have these hibernate mappings:




<hibernate-mapping>
<class name="gov.va.med.logService.hibernate.Distributionlist" table="DISTRIBUTIONLIST">
<id name="distributionlistid" type="java.lang.Long">
<column name="DISTRIBUTIONLISTID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">SDISTRIBUTIONLIST</param>
</generator>
</id>
<set name="listmembers" inverse="true">
<key>
<column name="DISTRIBUTIONLISTID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="gov.va.med.logService.hibernate.Listmember" />
</set>
</class>
</hibernate-mapping>



<hibernate-mapping>
<class name="gov.va.med.logService.hibernate.Listmember" table="LISTMEMBER">
<composite-id name="id" class="gov.va.med.logService.hibernate.ListmemberId">
<key-many-to-one name="distributionlist" class="gov.va.med.logService.hibernate.Distributionlist">
<column name="DISTRIBUTIONLISTID" precision="22" scale="0" />
</key-many-to-one>
<key-many-to-one name="serviceuser" class="gov.va.med.logService.hibernate.Serviceuser">
<column name="SERVICEUSERID" precision="22" scale="0" />
</key-many-to-one>
</composite-id>
</class>
</hibernate-mapping>





When I try to run I get this error:

Foreign key (FK5E33B8B89E456429:LISTMEMBER [DISTRIBUTIONLISTID])) must have same number of columns as the referenced primary key (LISTMEMBER [SERVICEUSERID,DISTRIBUTIONLISTID])



Could someone please tell me how to change my mapping to correct this error? Any help at all would be appreciated. Thanks.

- Ben
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would put a surrogate primary key called list_member_id in the list member join table. So that you don't have to do a composite Primary Key.

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic