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
MappingThe 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 TestCode:
<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 SentenceCode:
<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>