-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: key-many-to-one and one-to-one against the same table
PostPosted: Thu Feb 19, 2004 1:00 pm 
Beginner
Beginner

Joined: Mon Nov 24, 2003 12:44 pm
Posts: 40
I have a question about the way Middlegen-Hibernate has interpreted some foreign keys in my database schema. As can be seen below, the mapping generated for TIA00573 has both a "key-many-to-one" and a "one-to-one" mapping generated against TIA00571.
The database is such that the two tables are based on the same primary key but there will not always be rows present in TIA0573. For a row in TIA00573, there will always be a row in TIA00571, hence the constrained="true" setting in the TIA00573 mapping.
I understand from the DDL why Middlegen has generated a key-many-to-one, but I'm not sure it is actually correct as it is really a zero or one relationship. When I run a "from Tia00571" query, Hibernate throws an IllegalArgumentException for reasons I have yet to fathom out.

Any thoughts on this would be gratefully received.

Best Regards
Chris

Code:
-- DDL Statements for foreign keys on Table "CLEX   "."TIA00571"

ALTER TABLE "CLEX   "."TIA00571"
   ADD CONSTRAINT "FK01" FOREIGN KEY
      ("CNTR_ID")
   REFERENCES "COMMON "."TIA00569"
      ("CNTR_ID")
   ON DELETE RESTRICT
   ON UPDATE NO ACTION;

ALTER TABLE "CLEX   "."TIA00571"
   ADD CONSTRAINT "FK02" FOREIGN KEY
      ("CLAIM_CASE_NUMBER")
   REFERENCES "CLEX   "."TIA00570"
      ("CLAIM_CASE_NUMBER")
   ON DELETE RESTRICT
   ON UPDATE NO ACTION;

-- DDL Statements for foreign keys on Table "CLEX   "."TIA00573"

ALTER TABLE "CLEX   "."TIA00573"
   ADD CONSTRAINT "FK01" FOREIGN KEY
      ("CLAIM_CASE_NUMBER"   ,
       "CNTR_ID"      )
   REFERENCES "CLEX   "."TIA00571"
      ("CLAIM_CASE_NUMBER"   ,
       "CNTR_ID"      )
   ON DELETE RESTRICT
   ON UPDATE NO ACTION;


Middlegen generated mappings based on the above for TIA00571 and TIA00573...

Code:
<class name="Tia00571" table="TIA00571" schema="CLEX" proxy="Tia00571">

    <composite-id name="comp_id" class="Tia00571PK">
        <!-- bi-directional many-to-one association to Tia00570 -->
        <key-many-to-one name="tia00570" class="Tia00570">
           <column name="CLAIM_CASE_NUMBER" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to Tia00569 -->
        <key-many-to-one name="tia00569" class="Tia00569">
           <column name="CNTR_ID" />
       </key-many-to-one>
    </composite-id>   

    <property name="titleType" type="java.lang.String" column="TITLE_TYPE" length="1"/>
    <!-- etc... -->

    <!-- associations -->
    <!-- bi-directional one-to-one association to Tia00573 -->
    <one-to-one name="tia00573" class="Tia00573" outer-join="auto"/>
</class>
</hibernate-mapping>


<class name="Tia00573" table="TIA00573" schema="CLEX" proxy="Tia00573">

    <composite-id name="comp_id" class="Tia00573PK">
        <!-- bi-directional one-to-one association to Tia00571 -->
        <key-many-to-one name="tia00571" class="Tia00571">
           <column name="CLAIM_CASE_NUMBER" />
           <column name="CNTR_ID" />
       </key-many-to-one>
    </composite-id>   

    <property name="leadContractId" type="java.lang.String" column="LEAD_CONTRACT_ID" not-null="true" length="10"/>
    <!-- etc... -->

    <!-- associations -->
    <!-- bi-directional one-to-one association to Tia00571 -->
    <one-to-one name="tia00571" class="Tia00571" outer-join="auto" constrained="true"/>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Middlegen should not have generated the <key-many-to-one> and only the one-to-one. Compound keys are a pain :-(. Create an entry in JIRA. Ref this posting thread. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 7:31 am 
Beginner
Beginner

Joined: Mon Nov 24, 2003 12:44 pm
Posts: 40
Sorry David, before I add an entry to JIRA, I should point out that there were Primary Key definitions on both tables as follows...

Code:
ALTER TABLE "CLEX   "."TIA00571"
   ADD CONSTRAINT "PK" PRIMARY KEY
      ("CLAIM_CASE_NUMBER"     ,
       "CNTR_ID"      );

ALTER TABLE "CLEX   "."TIA00573"
   ADD CONSTRAINT "PK" PRIMARY KEY
      ("CLAIM_CASE_NUMBER"   ,
       "CNTR_ID"      );

Would you have expected to see the following generated in TIA00537's map...
Code:
    <composite-id name="comp_id" class="Tia00573PK">
        <!-- bi-directional many-to-one association to Tia00570 -->
        <key-many-to-one name="tia00570" class="Tia00570">
           <column name="CLAIM_CASE_NUMBER" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to Tia00569 -->
        <key-many-to-one name="tia00569" class="Tia00569">
           <column name="CNTR_ID" />
       </key-many-to-one>
    </composite-id>

...instead of
Code:
    <composite-id name="comp_id" class="Tia00573PK">
        <!-- bi-directional one-to-one association to Tia00571 -->
        <key-many-to-one name="tia00571" class="Tia00571">
           <column name="CLAIM_CASE_NUMBER" />
           <column name="CNTR_ID" />
       </key-many-to-one>
    </composite-id>   

Regards
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 1:25 pm 
Beginner
Beginner

Joined: Mon Nov 24, 2003 12:44 pm
Posts: 40
Is this actually an unusual case that would require a new Hibernate key type of...
Code:
    <key-one-to-one>

so my mapping could then become...
Code:
<composite-id name="comp_id" class="Tia00573PK">
    <!-- bi-directional one-to-one association to Tia00571 -->
    <key-one-to-one name="tia00571" class="Tia00571">
        <column name="CLAIM_CASE_NUMBER" />
        <column name="CNTR_ID" />
    </key-one-to-one>
</composite-id>


or am I barking up the wrong tree?

I keep thinking that a one-to-one association mapping in each map file ought to be sufficient, but then how would I express the composite primary key.
I tried setting the composite key in TIA00573 map to be the same as TIA00571, but just got NullPointerExceptions when I tried to run queries against them.
I will try and debug that version to see why that is happening, but any suggestions on how to express the table relationships in my map files would be greatly appreciated.

Regards
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 8:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The one to one mappings using primary keys for both sides as the join should not use the <key-xxxxx> tag. It is implied by the mapping one-to-one tag it self. So the key should just been a compound key. Now where we have a situation of foreign key one-to-one (bi-directional) then the <key-many-to-one> comes into play.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 5:05 am 
Beginner
Beginner

Joined: Mon Nov 24, 2003 12:44 pm
Posts: 40
Thanks for that. I will register and post the JIRA entry in the next day or so.

Cheers
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 9:37 am 
Beginner
Beginner

Joined: Mon Nov 24, 2003 12:44 pm
Posts: 40
Solved. see post...

http://forum.hibernate.org/viewtopic.ph ... highlight=

Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.