Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.2
Hi i have interesting problem not yet solved which is relationship between two tables have CPK's(Composit Primary Keys) and back to back reference to each other.
Here is the problem described Crearly i have two table were both the tables have CPK were activity test has 4 keys, and batch has 2 keys
"batch_id" from activity test is the FK(Foreign key) refering to "batch" table which is part of batch CPK.
Tabel1:
ActivityTest
activity_test_id|
batch_id |
period_activity_id |
answer_map_id |col1 |. .|coln|
Table2: Batch
batch_id|
activity_test_id|col1|col2|... .....|coln-1|coln|
Mapping documents:
Mapping file for ActivityTest
Code:
<hibernate-mapping>
<class name="hibernateorm.Batch" table="batch">
<composite-id name="batchCPK" class="hibernateorm.BatchCPK">
<key-property name="batchId" column="batch_id" type="int"/>
<key-property name="activityTest" column="activity_test_id"/>
</composite-id>
<many-to-one name="activityTest" class="hibernateorm.ActivityTest" column="activity_test_id" insert="false" update="false"/>
<property name="activityTest" type="int" column="batch_data"/>
</class>
</hibernate-mapping>
ActivityTest Mapping File:
Code:
<hibernate-mapping>
<class name="hibernateorm.ActivityTest" table="activity_test">
<composite-id name="activityTestCPK" class="hibernateorm.ActivityTestCPK">
<key-property name="activityTestId" type="int" column="activity_test_id"/>
<key-property name="batch" column="batch_id"/>
<key-property name="periodActivity" column="period_activity_id"/>
<key-property name="testDefinition" column="test_definition_id"/>
<key-property name="answerMap" column="answer_map_id"/>
</composite-id>
<many-to-one class="hibernateorm.Batch" property-ref="batchId" name="batch" column="batch_id" insert="false" update="false"/>
<many-to-one class="hibernateorm.PeriodActivity" name="periodActivity" column="period_activity_id" insert="false" update="false"/>
<many-to-one class="hibernateorm.TestDefinition" name="testDefinition" column="test_definition_id" insert="false" update="false"/>
<many-to-one class="hibernateorm.AnswerMap" name="answerMap" column="answer_map_id" insert="false" update="false"/>
<property name="dateTime" type="java.sql.Timestamp" column="activity_test_datetime"/>
</class>
</hibernate-mapping>
hibernate exception :now when i execute get the following error :
org.hibernate.MappingException: Foreign key (FKC23D9BC291A06744:activity_test [batch_id])) must have same number of columns as the referenced primary key (batch [batch_id,activity_test_id])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1263)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:756)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:93)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:61)
now my question is
how we can refer to batchCPK.batch_id in ActivityTest maping ?
can this be solved in hibernate ...?
any hibernate experts can answer this ..?
Advance thanks for any reply on it :).