2011-11-14 25 views
5

另一個NHibernate JOIN問題。NHibernate - 通過不同的鍵將多個JOIN加入同一個表

我試圖通過不同的兩個 鍵從一個表加入兩個不同的屬性。 但我無法獲得第二個JOIN屬性。

簡單的例子 -

我的班級 -

namespace Domain 
{ 
    public class Message 
    { 
     #region private Members 

     private string _id; 
     private string _senderID; 
     private string _recipientID; 
     private string _recipientName; 
     private string _senderName; 

     #endregion 

     #region Public Properties 

     public virtual string ID 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     public virtual string ID 
     { 
      get { return _id; } 
      set { _id = value; } 
     } 

     public virtual string SenderID 
     { 
      get { return _senderID; } 
      set { _senderID= value; } 
     } 

     public virtual string RecipientID 
     { 
      get { return _recipientID; } 
      set { _recipientID= value; } 
     } 

     public virtual string SenderName 
     { 
      get { return _senderName; } 
      set { _senderName= value; } 
     } 

     public virtual string RecipientName 
     { 
      get { return _recipientName; } 
      set { _recipientName= value; } 
     } 

     #endregion 

     #region Constructors 

     public Message() 
     { 
      _id = Guid.NewGuid().ToString(); 
     } 

     #endregion 
    } 
} 

映射 -

<class name="Domain.Message" table="Messages" > 
    <id name="ID"> 
     <column name="OID"/> 
     <generator class="assigned"/> 
    </id> 
    <property name="SenderID" unique="true"> 
     <column name="SenderID" unique="true"/> 
    </property> 
    <property name="RecipientID" unique="true"> 
     <column name="RecipientID" unique="true"/> 
    </property> 
    <join table="CompanyData" optional="true" > 
     <key column="CompanyID" property-ref="SenderID" /> 
     <property name="SenderName" column="CompanyName" unique="true" lazy="false"/> 
    </join> 
    <join table="CompanyData" optional="true" > 
     <key column="CompanyID" property-ref="RecipientID" /> 
     <property name="RecipientName" column="CompanyName" unique="true" lazy="false"/> 
    </join> 
</class> 

,但我得到了下面的SQL -

SELECT this_.OID as OID30_0_, this_.SenderID as Sender30_0_, 
this_.RecipientID as Recipient30_0_, this_1_.CompanyName as SiteID9_0_ 
FROM Messages this_ 
left outer join CompanyData this_1_ on 
this_.SenderID=this_1_.CompanyID 
left outer join CompanyData this_2_ on 
this_.RecipientID=this_2_.CompanyID 

而且我想 -

SELECT this_.OID as OID30_0_, this_.SenderID as Sender30_0_, 
this_.RecipientID as Recipient30_0_, this_1_.CompenyName as 
SiteID9_0_ , this_2_.CompanyName as SiteID10_0_ 
FROM Messages this_ 
left outer join CompanyData this_1_ on 
this_.SenderID=this_1_.CompanyID 
left outer join CompanyData this_2_ on 
this_.RecipientID=this_2_.CompanyID 

我使用NHibernate 3.2

感謝

+0

我看不出什麼毛病的映射。你可以發佈課程代碼嗎? –

+0

什麼是RecipientName運行時填充的? SenderName值?我傾向於認爲這可能是一個錯誤。 nHibernate可能會嘗試爲您正確優化。 –

+0

是的,您是對的,RecipientName填充了SenderName值。有什麼我可以做關閉優化? –

回答

2

顯然,這不能與NHibenate映射瞬間完成。 Spencer Ruport建議的最接近的解決方案是創建視圖並將對象映射到該視圖。

3

我整天都在網上潛伏着尋找解決同樣問題的方法。 我發現的是hibernate board上的線程。我從Ashkan Aryan獲得瞭解決方案。因此,如果其他人對解決方案感到頭疼,並且不想使用視圖,我會發布我現在使用的代碼。

我必須在同一張桌子上使用1到12個連接,所以創建視圖會讓人困惑。

private void addParagraphsQuery(DetachedCriteria sourceQuery, List<ParagraphContentArgument> paragraphsArguments) 
{ 
    DetachedCriteria dc; 
    Conjunction conjunction = Restrictions.Conjunction(); 
    string alias = string.Empty; 

    if (paragraphsArguments != null && paragraphsArguments.Count > 0) 
    { 
     for (int i = 0; i < paragraphsArguments.Count; i++) 
     { 
      alias = "p" + i.ToString(); 
      dc = DetachedCriteria.For<Document>().SetProjection(Projections.Id()); 
      dc.CreateAlias("paragraphList", alias); 
      dc.Add(Restrictions.Eq(alias + ".paragraphSectionTemplate", paragraphsArguments[i].ParagraphTemplate)); 
      dc.Add(Restrictions.Like(alias + ".content", paragraphsArguments[i].Argument, MatchMode.Anywhere)); 
      conjunction.Add(Property.ForName("id").In(dc)); 
     } 
    } 
    sourceQuery.Add(conjunction); 
} 

問候,

的Mariusz