-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping a 1-many on natural keys rather than assigned PK
PostPosted: Thu Oct 21, 2004 2:33 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
We have 2 tables between which there is a 1-many relationship, TableA to TableB. Both tables already have surrogate PK columns, and have other relationships to other tables joining on their PKs are normal.

However, these tables also have a natural key, and we want the mapping to be done using this natrual key instead.

For example:
Code:
TableA:
TableA_ID : bigint
AIdentifier: varchar(20) //natural key
a_1 : varchar(10)
a_2: varchar(10)

TableB:
TableB_ID : bigint
AIdentifier: varchar(20) //FK to TableA's natural key
b_1 : varchar(10)
b_2: varchar(10)



The Hibernate docs describe how to map this many-to-one not using the primary key but by using a property-ref, so in the mapping for TableB business object we have:

Code:
   <many-to-one
      name="tableA"
      property-ref="AIdentifier"
      column="AIdentifier"
   />


How do you map the reverse of this relationship as a set, so that it is joining on this additional natural key instead of using the PK?

So in our mapping for Table A we currently have:
Code:
    <set
        name="tableBs"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="AIdentifier" />
        </key>
        <one-to-many
            class="TableB"
        />
    </set>



If we try and obtain the collection of TableBs from TableA, the SQL that is generated is incorrectly joining on the PK from TableA to the FK in TableB that is the natural key, but what we need to do is join on the natural key in TableA the FK reference to the natrual key in TableB.

Any ideas?

Thanks,
Kevin Hooke

Hibernate version: 2.1.6

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
DB2 UDB 8.1.6
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


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

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.