Solved

Hibernate composite key/FK error

Posted on 2009-05-13
2
436 Views
Last Modified: 2015-01-05
I ended up with this error and couldn't figure out why..need some ideas to solve this..

 org.hibernate.MappingException: Foreign key (FKC4944AB4EC08D0E:CP_PROJ_CAND_USID [candidates_PROJ_NO_SEQ,candidates_SLT_USIDIDENTITY])) must have same number of columns as the referenced primary key (CP_PROJ_CAND_USID [PROJ_NO_SEQ,candidates_PROJ_NO_SEQ,candidates_SLT_USIDIDENTITY])


 <composite-id class="com.xxx.yyy.zzz.CpProjCandUsid$CompositeId"  name="compositeId">
      <key-property type="java.lang.Long" column="PROJ_NO_SEQ" length="10" name="projNoSeq"/>
      <key-property type="java.lang.Long" column="SLT_USIDIDENTITY" length="10" name="usid"/>
    </composite-id>
<many-to-one not-null="true" name="project" class="com.xxx.yyy.zzz.Project" insert="false" update="false">
0
Comment
Question by:praveen009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
jocafi earned 500 total points
ID: 24384798
What are u trying to do? A Bilateral-Association between 2 tables ?

I read the Hibernate manual and they say it is a bad strategy to use <composite-id>. Therefore, if you can design better your database model you are lucky.

I understood that you want a table that has the user ID and project ID, correct ?

Try to use Uni-Directional Association or Bi-Directional Association. In the hbm file of the user you write:

    <set name="userProjects" table="UserProjects">
      <key column="SLT_USIDIDENTITY"/>
      <many-to-many class="com.xxx.yyy.zzz.Project" column="PROJ_NO_SEQ"/>
    </set>

It creates automtically a new table called UserProjects that has the user id and project id as primary key.

If you want a bi-directional association do the same in the hbm file of the project:

    <set name="userProjects" table="UserProjects">
      <key column="PROJ_NO_SEQ"/>
      <many-to-many class="com.xxx.yyy.zzz.User" column="SLT_USIDIDENTITY"/>
    </set>

More info under:
http://www.guruja.co.cc/index.php/Hibernate#Mapping_associations
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Viewers will learn how to use the Hootsuite Dashboard.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question