[jboss-jira] [JBoss JIRA] (HIBERNATE-118) one-to-one with composite-id must have common CompositeId class

Steve Ebersole (JIRA) issues at jboss.org
Thu Nov 19 10:32:00 EST 2015


    [ https://issues.jboss.org/browse/HIBERNATE-118?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13130919#comment-13130919 ] 

Steve Ebersole commented on HIBERNATE-118:
------------------------------------------

This is not the correct project to report Hibernate issues under. This Jira project is for reporting issues in the integration between Hibernate and WildFly. To report issues in Hibernate, please use https://hibernate.atlassian.net. Thanks.


> one-to-one with composite-id must have common CompositeId class
> ---------------------------------------------------------------
>
>                 Key: HIBERNATE-118
>                 URL: https://issues.jboss.org/browse/HIBERNATE-118
>             Project: Hibernate Integration
>          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 was sent by Atlassian JIRA
(v6.4.11#64026)


More information about the jboss-jira mailing list