NHibernate - 通过不同的键多次JOIN到同一个表

时间:2011-11-14 08:14:12

标签: c# .net nhibernate nhibernate-mapping

另一个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

由于

2 个答案:

答案 0 :(得分: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);
}

此致

马里乌什

答案 1 :(得分:2)

显然,目前NHibenate映射无法做到这一点。 Spencer Ruport建议的最接近的解决方案是创建一个视图并将对象映射到它。