NHibernate的 - 多个连接到同一个表通过不同的密钥 [英] NHibernate - multiple JOIN to the same table by different keys

查看:131
本文介绍了NHibernate的 - 多个连接到同一个表通过不同的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个NHibernate的JOIN问题。

Another NHibernate JOIN problem.

我想用两个不同从一台连接两个不同的属性 键。 但我不能得到第二个连接属性。

I'm trying to join two different properties from one table by different two keys. But I can't get the second JOIN property.

简单的例子 -

我的阶级 -

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

映射 -

Mapping -

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

but I get the following 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

I'm using NHibernate 3.2

感谢

推荐答案

显然,这不能在此刻与NHibenate映射完成。 最近的解决方案,通过斯宾塞Ruport 的建议,就是要创建一个视图和对象映射到它。

Apparently, this cannot be done at the moment with NHibenate mappings. The closest solution, suggested by Spencer Ruport, is to create a view and map the object to it.

这篇关于NHibernate的 - 多个连接到同一个表通过不同的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆