I've got a simple parent/child relationship between two tables/objects but both tables have composite IDs. I'm able to query them but the joins are not correct.
Here are the details...
parent mapping:
Code:
<hibernate-mapping>
<class
name="PactsSalesHeader"
proxy="PactsSalesHeader"
table="pa_invhdrp">
<composite-id name="Id" class="PactsSalesHeaderId">
<key-property name="InvoiceNumber" column="ihinvno" />
<key-property name="CompanyCode" column="comcode" />
</composite-id>
<property name="BranchId" column="brid"/>
<property name="DateCreated" column="ihcrtdte"/>
<property name="InvoiceTotal" column="ihinvtot"/>
<bag
name="Branches"
table="pa_branchp"
inverse="true"
lazy="true"
fetch="join"
cascade="all-delete-orphan">
<key>
<column name="brid" />
<column name="comcode" />
</key>
<one-to-many class="PactsBranch" />
</bag>
</class>
</hibernate-mapping>
child mapping:
Code:
<hibernate-mapping>
<class
name="PactsBranch"
proxy="PactsBranch"
table="pa_branchp">
<composite-id name="Id" class="PactsBranchId">
<key-property name="BranchId" column="brid" />
<key-property name="CompanyCode" column="comcode" />
</composite-id>
<property name="Name" column="brname"/>
<many-to-one
name="Header"
insert="false"
update="false"
class="PactsSalesHeader">
<column name="brid" />
<column name="comcode" />
</many-to-one>
</class>
</hibernate-mapping>
The query that is returned:
Code:
select
this_.ihinvno as ihinvno17_1_,
this_.comcode as comcode17_1_,
this_.brid as brid17_1_,
this_.ihcrtdte as ihcrtdte17_1_,
this_.ihinvtot as ihinvtot17_1_,
branches2_.brid as brid3_,
branches2_.comcode as comcode3_,
branches2_.brid as brid18_0_,
branches2_.comcode as comcode18_0_,
branches2_.brname as brname18_0_
from
pa_invhdrp this_
left outer join
pa_branchp branches2_
on
[b]this_.ihinvno=branches2_.brid[/b]
and
this_.comcode=branches2_.comcode
Notice the join criteria...it's not joining the correct fields...I would have expected this query:
Code:
select
this_.ihinvno as ihinvno17_1_,
this_.comcode as comcode17_1_,
this_.brid as brid17_1_,
this_.ihcrtdte as ihcrtdte17_1_,
this_.ihinvtot as ihinvtot17_1_,
branches2_.brid as brid3_,
branches2_.comcode as comcode3_,
branches2_.brid as brid18_0_,
branches2_.comcode as comcode18_0_,
branches2_.brname as brname18_0_
from
pa_invhdrp this_
left outer join
pa_branchp branches2_
on
[b]this_.brid=branches2_.brid[/b]
and
this_.comcode=branches2_.comcode
Even though I specified the key fields to join on...it's using the primary key(s) from the parent mapping (or so it appears.)
What am I doing wrong?
Thanks!
Hibernate version: 3.1
Name and version of the database you are using: MSSQL 2000