如何在使用NHibernate JoinQueryOver时指定要进行连接的键

时间:2012-09-24 22:14:23

标签: c# sql nhibernate fluent

public class Store
{
public virtual prop1 { get; set;}
public virtual int RegionID { get; set;}
public virtual Region Region { get; set;
}

public class Region
{
  public virtual int RegionID { get; set;}
}

最后我的sql应该看起来像

select * from store inner join region on store.regionid = region.regionid

如何让上面的内容实现上面的内部联接。

Session.QueryOver<Store>()
                .JoinQueryOver<Region>(s => s.Region)

1 个答案:

答案 0 :(得分:0)

要获得内部联接,必须在NHibernate映射中设置Store实体和Region实体之间的关系。

您无法使用QueryOver API指定要加入的密钥。

您的课程应与此类似:

public class Region
{
  public virtual int RegionID { get; set;}
  public virtual IList<Store> Stores { get; set; }
}

public class Store
{
    public virtual int StoreId { get; set;}
    public virtual Region Region { get; set; }
}

你的映射应该与此类似:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="YourNameSpace.Region, YourNameSpace" table="Region">
    <id name="RegionID">
      <column name="RegionID" />
      <generator class="identity" />
    </id>
    <bag cascade="all" inverse="true" name="Stores">
      <key>
        <column name="RegionID" />
      </key>
      <one-to-many class="YourNameSpace.Store, YourNameSpace" />
    </bag>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="YourNameSpace.Store, YourNameSpace" table="Store">
    <id name="StoreID">
      <column name="StoreID" />
      <generator class="identity" />
    </id>

    <many-to-one class="YourNameSpace.Region, YourNameSpace" name="Region">
      <column name="RegionId" />
    </many-to-one>
  </class>
</hibernate-mapping>