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: