-->
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.  [ 1 post ] 
Author Message
 Post subject: mapping exception many-to-one
PostPosted: Tue Feb 09, 2010 4:12 am 
Beginner
Beginner

Joined: Sat Dec 01, 2007 4:34 pm
Posts: 20
I'm trying to map several tables in hibernate, but I keep getting the following error:
org.hibernate.MappingException: Foreign key (FKBEC21710D4120975:TestAnswer [TestSentenceId])) must have same number of columns as the referenced primary key (TestSentence
[TestId,SentenceId])


In the Test table, when I remove the mapping for sentenceList it works fine... Can somebody find the error?

Here are my tables:
Test
TestId (PK)
TestDate

Sentence
SentenceId (PK)
Phrase
RightPhrase

TestSentence
TestSentenceId (PK)
SentenceId (FK)
TestId (FK)

TestAnswer
TestAnswerId (PK)
TestSentenceId (FK)
Answer

Mapping
The error appears when I try to map the foreign key TentSentenceId in TestAnswer with primary key TestSentenceId in TestSentence. Is this because the two other fields in TestSentence: SentenceId and TestId are both foreign keys? Should I make it a composite key?

Mapping for Test
Code:
<hibernate-mapping>
  <class name="db.Test" table="Test">
    <id column="TestId" name="testId" type="long">
      <generator class="native"/>
    </id>
    <property column="TestDate" name="testDate" type="date"/>

    <set name="sentenceList" cascade="all" lazy="false" table="TestSentence">
      <key column="TestId"/>
      <many-to-many class="db.Sentence" column="SentenceId"/>
    </set>
  </class>
</hibernate-mapping>


Mapping for Sentence

Code:
<hibernate-mapping>
  <class name="db.Sentence" table="Sentence">
    <id column="SentenceId" name="sentenceId" type="long">
      <generator class="native"/>
    </id>
    <property column="Phrase" name="phrase" type="string"/>
</hibernate-mapping>


Mapping for the TestAnswer table:
Code:
<hibernate-mapping>
  <class name="db.TestAnswer" table="TestAnswer">
    <id column="TestAnswerId" name="testAnswerId" type="long">
      <generator class="native"/>
    </id>
<many-to-one class="db.TestSentence" column="TestSentenceId" name="testSentence" not-null="true" /> This is what causes the error!
    <property column="Answer" name="answer" type="string"/>
  </class>
</hibernate-mapping>


Mapping for the TestSentence table:
Code:
<hibernate-mapping>
  <class name="db.TestSentence" table="TestSentence">
    <id column="TestSentenceId" name="testSentenceId" type="long">
      <generator class="native"/>
    </id>
    <property column="SentenceId" name="sentenceId" type="long"/>
    <property column="TestId" name="testId" type="long"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.