[jboss-jira] [JBoss JIRA] Created: (HIBERNATE-118) one-to-one with composite-id must have common CompositeId class
Sandeep Vaid (JIRA)
jira-events at lists.jboss.org
Fri Jul 3 09:31:51 EDT 2009
one-to-one with composite-id must have common CompositeId class
---------------------------------------------------------------
Key: HIBERNATE-118
URL: https://jira.jboss.org/jira/browse/HIBERNATE-118
Project: Hibernate
Issue Type: Bug
Reporter: Sandeep Vaid
Assignee: Steve Ebersole
One query before asking this question:
I can see 2 different sites for Hibernate Bug Tracking:
1) Hibernate JIRA - http://opensource.atlassian.com/projects/hibernate/secure/Dashboard.jspa
2) JBOSS JIRA- This site
Which is the official site? Out of these 2 sites on which i should post the bugs etc.. ?
I have one-to-one relationship between Product and ProductBasic..
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..
NOTE: This is the similar exception if i don't use a seperate class for CompositeId..
Hibernate docs have a statement about this situation as :
Unfortunately, this approach to composite identifiers means that a persistent object is its own identifier. There is no convenient "handle" other than the object itself. You must instantiate an instance of the persistent class itself and populate its identifier properties before you can load() the persistent state associated with a composite key. We call this approach an embedded composite identifier, and discourage it for serious applications.
A second approach is what we call a mapped composite identifier, where the identifier properties named inside the <composite-id> element are duplicated on both the persistent class and a separate identifier class.
<composite-id class="MedicareId" mapped="true">
<key-property name="medicareNumber"/>
<key-property name="dependent"/>
</composite-id>
Useful Links : https://forum.hibernate.org/viewtopic.php?f=1&t=993886
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the jboss-jira
mailing list