one-to-one with composite-id must have common CompositeId class

Description

I have one-to-one relationship between Product and Product-Id..

I have Product (PId, PDesc) as composite-key
I have ProductBasic (ProductId, ProductDesc) as composite-Key

Case 1: I created one Composite class which is common for both Product and ProductBasic..
class CompProductRelated{
string pid;
string pdesc;
}
then try to fetch Product.. It worked !!!

Case 2: If i create seperate Composite class for Product & ProductBasic viz. CompProduct and CompProductBasic respectively as :

class CompProduct{
string productId;
string productDesc;
}

and not if i define the mapping accordingly as:
In Product.hbm.xml
<composite-id name="compProduct" class="CompProduct">
<key-property name="pid" type="string" column="PRODUCTID" length="10"></key-property>
<key-property name="desc" type="string" column="DESC" length="10"></key-property>
</composite-id>

In ProductBasic.hbm.xml
<composite-id name="compProductBasic" class="CompProductBasic">
<key-property name="productId" column="PRODUCTID" type="string" length="10"/>
<key-property name="productDesc" column="DESC1" type="string" length="10"/>
</composite-id>
Now while fetching Product (and internally it will fetch ProductBasic), it gives the following exception:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class model.CompProductBasic, got class model.CompProduct

So the only approach for one-to-one with composite-id is to have a common compositeId class..
The link http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#mapping-declaration-compositeid
recommends 3 solutions (3rd being to have a common compositeId class) but as per my understanding 1st and 2nd approach will give exception for one-to-one with composite-id ...

I think this hibenate restriction (of having common CompositeId class for one-to-one relationship) is not good as it puts a constraint on how domain model (java) should be which could not be possible everytime in legacy applications..

Activity

Show:

Christian Beikov October 29, 2020 at 9:25 AM

I just ran into the issue myself in 5.3.18 and I found out that there are a few possible workarounds:

  • Use the same composite id type in both entities

  • Use a bidirectional mapping and use mappedBy

Nikhil Kale May 27, 2020 at 12:43 PM

Hello,

Is there any update on this issue? I am facing this issue in a legacy application where we are restricted in terms of what we could change with respect to the domain model.
Can you please provide update and indicate if/when this issue would be fixed?

Thank you.

Former user October 4, 2016 at 4:17 PM

Is there a reason why this issue is still not fixed?

Details

Assignee

Reporter

Priority

Created June 26, 2009 at 8:40 AM
Updated October 29, 2020 at 9:26 AM