• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Relationship between two table having composite keys on both the sides

 
Rajesh Giriyappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two tables called "ActivityTest and "Batch" were both carrying Composite Keys in DB.
the mapping file for the 'ActivityTest' is as follows:
<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-many-to-one class="hibernateorm.PeriodActivity" name="periodActivity"/>
<key-many-to-one class="hibernateorm.Batch" name="batch" column="batch_id"/>
</composite-id>
<property name="dateTime" type="java.sql.Timestamp" column="activity_test_datetime"/>
.....
</hibernate-mapping>


and Batch mapping file looks like:

<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-many-to-one name="activityTest" class="hibernateorm.ActivityTest" column="activity_test_id"/>

</composite-id>
<property name.../>
..........
</hibernate-mapping>


because foreign key "batch" itself is composite Key ,hibernate throws error
as org.hibernate.MappingException can some body help on this ! advance thanks for the help.

[ Edited to remove abbreviation in topic title - Paul Sturrock ]
[ March 05, 2007: Message edited by: Paul Sturrock ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh, can you repost your mapping within the CODE tags. There are buttons underneath the Add Reply button, one is called CODE, and that way it keeps your indentation and makes it easier to read. I was trying to read your composite-id but it was difficult without indentation.

Thanks

Mark
 
Rajesh Giriyappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark here is my CODE reformatted as you said
Code for ActivityTest Map File


Code For Batch

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I see.

The Java Persistence with Hibernate book actually suggests a different approach. Instead of using <key-many-to-one in your composite-id. Use <key-property> and add a regualr <many-to-one> mapping but set insert="false" and update="false"

So in your case it would look like this



So have activityTest attribute be of type ActivityTest and call getBatchData() to get the data.

Mark
 
Rajesh Giriyappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Done with that ,
Hibernate Replies saying:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic