我寫的代碼實際上是它的工作,但它不是非常優化的。NHibernate:用左連接代替多對一
在我的例子,我有兩個很簡單的表
一個簡單的SQL查詢來獲取這個人的名字和相關的國家名稱(如果有的話)應該是這樣的
SELECT Person.Name AS Name, Country.CountryName AS Country FROM Person LEFT OUTER JOIN Country ON Country.CountryId = Person.CountryId
而且一個構造良好的對象用NHibernate拾取數據看起來像這樣
夠簡單了吧。我的模型看起來像這樣
namespace NHibernateLeftJoin.Models
{
public class Country
{
public virtual int CountryId { get; set; }
public virtual string CountryName { get; set; }
}
public class Person
{
public virtual int PersonId { get; set; }
public virtual string Name { get; set; }
public virtual int CountryId { get; set; }
public virtual Country CountryObject { get; set; }
}
}
而且映射
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="NHibernateLeftJoin.Models.Country, NHibernateLeftJoin" lazy="true">
<id name="CountryId">
<generator class="native" />
</id>
<property name="CountryName" />
</class>
<class name="NHibernateLeftJoin.Models.Person, NHibernateLeftJoin" lazy="true">
<id name="PersonId">
<generator class="native" />
</id>
<property name="Name" />
<property name="CountryId" />
<many-to-one name="CountryObject" class="NHibernateLeftJoin.Models.Country" lazy="false"
column="CountryId" outer-join="true" unique="true" not-null="false" cascade="none" />
</class>
</hibernate-mapping>
這種方法的問題是,NHibernate的取一個「人」行所說的「Contry」表的每一行,如果我們有成千上萬的行不太好。似乎不可能像我們使用SQL一樣完美地執行此操作,或者它使用完全錯誤的方法。
的VS項目可以在這裏找到有興趣的人士https://dl.dropboxusercontent.com/u/6208162/NHibernateLeftJoin.zip
感謝
== ==編輯
查詢可能看起來像這樣
private static IList<Person> GetPersons()
{
using (var session = NHibernateHelper.OpenSession())
{
//using (var transaction = session.BeginTransaction()) {}
IQuery query = session.CreateQuery("FROM Person");
return query.List<Person>();
}
}
的sql由此生成將看起來像這樣
NHibernate: select person0_.PersonId as PersonId1_, person0_.Name as Name1_, person0_.CountryId as CountryId1_ from Person person0_
NHibernate: SELECT country0_.CountryId as CountryId0_0_, country0_.CountryName as CountryN2_0_0_ FROM Country country0_ WHERE [email protected];@p0 = 1 [Type: Int32 (0)]
NHibernate: SELECT country0_.CountryId as CountryId0_0_, country0_.CountryName as CountryN2_0_0_ FROM Country country0_ WHERE [email protected];@p0 = 2 [Type: Int32 (0)]
NHibernate: SELECT country0_.CountryId as CountryId0_0_, country0_.CountryName as CountryN2_0_0_ FROM Country country0_ WHERE [email protected];@p0 = 3 [Type: Int32 (0)]
這對我的數據非常合乎邏輯,第一個查詢取得所有人員,然後用三個不同的查詢(有三個唯一的國家映射到用戶)獲取CountryObject的數據。
感謝
有沒有嘗試,它仍然執行多個查詢 –
如何與QueryOver解決這個任何想法? :) –
@Eric赫利茨您可以發佈您的查詢代碼和生成的SQL? – dotjoe