2011-10-31 47 views
3

Stackoverflow的第一個問題。NHibernate中的多重連接映射

在使用JOIN映射屬性後,我嘗試將該屬性用於第三個表的另一個連接。問題是在生成的SQL中,第二個JOIN語句使用正確的列,但是來自原始表而不是第二個表。

這裏的映射 -

<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> 

生成的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 

我需要以下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 

我使用NHibbernate 3.2。

謝謝

+0

組件與NetHistoryMessage是1:1或1:M的關係嗎? –

+0

這些是1:1的關係。 –

回答

1

我試過一樣,但從來沒有得到它的工作。 <join>僅用於連接原始表,而不是級聯。更好的是將DunsSite聲明爲實體,並使用<join>從那裏訪問組件。那麼你可以在NetHistoryMessage上引入Convenienceproperties。

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

我試圖避免使用這個選項,因爲很難在ComponentName屬性上運行查詢。 –

+0

然後數據庫視圖會更好這個 – Firo

+0

你是對的,它會解決我眼前的問題,但我相信這個能力應該被整合到NHibernate。如果我有一個「棕色領域」,我必須創建一個可更新的對象,我不能用我的對象映射到一個視圖。 –