我試圖用nhibernate條件執行左外連接。我也有一個適用於我的查詢的過濾器。在應用nhibernate過濾器時執行左外連接
我遇到的問題是,如果連接結果爲空,過濾器會停止左外連接正常工作。
作爲一個非常簡單的例子,我想回到所有的音樂家,如果他們是一個樂隊,然後還自己帶
NHibernate的生成以下SQL
SELECT this_.Name, band2_.Name
FROM Musicians this_
left outer join [Band] band2_
on this_.BandID = band2_.ID
WHERE (band2_.IsDeleted = 0)
這將不會返回音樂家,如果他們不在樂隊。我想要的東西就像
SELECT this_.Name, band2_.Name
FROM Musicians this_
left outer join [Band] band2_
on this_.BandID = band2_.ID
WHERE this_.ID = 4894 /* @p3 */
(band2_.ID IS NULL OR band2_.IsDeleted = 0)
這是可能與nhibernate?
UPDATE
var projections = new[]
{
Projections.Property("Musician.Name").As("MusicianName"),
Projections.Property("Band.Name").As("BandName")
};
return this.sessionProvider.GetSession().CreateCriteria<Musician>("Musician")
.CreateCriteria("Musician.Band", "Band", JoinType.LeftOuterJoin)
.SetProjection(projections)
.Add(Restrictions.Eq("Musician.ID", parameters.MusicianId))
.SetResultTransformer(Transformers.AliasToBean<MusicianDetailsResult>())
.UniqueResult<MusicianDetailsResult>();
過濾器定義與FluentNHibernate
this.WithName(FilterName).WithCondition("IsDeleted = 0")
你能發佈ICriteria的代碼嗎? – DanP 2010-09-01 13:45:19