Mr Mush
Mr Mush

Reputation: 1538

Multiple Join Mappings in NHibernate

First question on Stackoverflow.

After using JOIN to map a property I try to use the property for another join from a third table. The problem is that in the generated SQL the second JOIN statement uses the correct column but from the original table and not the second table.

Here's the mapping -

 <class name="Core.Domain.NetHistoryMessage, Core" table="NHistoryIN" >
<id name="ID">
  <column name="ID"/>
  <generator class="assigned"/>
</id>     
<property name="RecipientDuns" unique="true">
  <column name="Recipient" unique="true"/>
</property>
<join table="DunsSites" optional="true" fetch="select">
  <key column="Duns" property-ref="RecipientDuns" />
  <property name="RecipientID" column="SiteID" unique="true" lazy="false"/>
</join>
<join table="Components"  optional="true" >
  <key column="ComponentID"   property-ref="RecipientID" />
  <property name="RecipientName" column="ComponentName" unique="true" lazy="false"/>
</join> 

The generated SQL -

SELECT this_.*, this_1_.SiteID as SiteID7_0_, this_2_.M_SNAME as M3_11_0_ 
FROM RNTransactionHistoryIN this_ 
left outer join DunsSites this_1_ on this_.Recipient=this_1_.Duns 
left outer join Components this_2_ on this_.SiteID=this_2_.ComponentID

I need the following SQL -

SELECT this_.*, this_1_.SiteID as SiteID7_0_, this_2_.M_SNAME as M3_11_0_ 
FROM RNTransactionHistoryIN this_ 
left outer join DunsSites this_1_ on this_.Recipient=this_1_.Duns 
left outer join Components this_2_ on this_1_.SiteID=this_2_.ComponentID

I'm using NHibbernate 3.2.

Thanks

Upvotes: 4

Views: 675

Answers (1)

Firo
Firo

Reputation: 30803

i tried the same but never got it to work. <join> is meant to only join from the original table, not cascading. better you declare DunsSite as an Entity and use <join> from there to Components. then you can introduce Convenienceproperties on NetHistoryMessage.

public string ComponentName
{
    get { return DunsSite.ComponentName; }
    set { DunsSite.ComponentName = value; }
}

Upvotes: 1

Related Questions