1
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)
4

1 回答 1

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>
于 2012-09-24T23:38:21.913 回答