Forums Register Login

Relationship between two table having composite keys on both the sides

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Mark here is my CODE reformatted as you said
Code for ActivityTest Map File


Code For Batch

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Mark Done with that ,
Hibernate Replies saying:

Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1602 times.
Similar Threads
[Hibernate] Composite key one-to-many XML Error
Exception when having different class and table name
MappingException: Could not read mappings from resource
Classcast exception
problem with hibernate mapping
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:55:09.