2012-07-11 54 views
3

我有一個實體GameSystemDAO和實體ContestPlanningGSItemDAO屬性GameSystem這是一個多對一的GameSystemDAO類型。什麼是QueryOver表達式,它對應於以下SQL?NHibernate不存在與QueryOver

select * 
from gamesystemdao g 
where not exists (
    select * 
    from contestplanninggsitemdao cpgsi 
    where cpgsi.gamesystem = g.id) 

我嘗試了以下內容(以及許多其他變化):

GameSystemDAO gameSystemAlias = null; 
ContestPlanningGSItemDAO contestPlanningGSItemAlias = null; 
List<GameSystemDAO> newGameSystems = session.QueryOver<GameSystemDAO>(() => gameSystemAlias) 
        .WithSubquery 
        .WhereNotExists(
         QueryOver.Of<ContestPlanningGSItemDAO>(() => contestPlanningGSItemAlias) 
         .Where(() => contestPlanningGSItemAlias.GameSystem.Id == gameSystemAlias.Id) 
         .Select(c => c.GameSystem)) 
        .List(); 

但總是得到KeyNotFoundException:給定的關鍵是不存在的字典。看起來NHibernate正在ContestPlanningGSItemDAO實例上尋找名爲gameSystemAlias的屬性。

我在做什麼錯?

+0

一個完整的stacktrace最經常幫助很多,所以plz總是從開始發佈 – Firo 2012-07-12 09:01:37

+0

@Firo異常正在拋出NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(字符串propertyName),第37行(使用NHibernate的3.2.0) – Marto 2012-07-12 10:24:38

+0

感謝發佈,這讓我走了。 – JasonCoder 2013-07-02 22:17:32

回答

0

我發現,在更新到3.3.1的NHibernate得到這個工作;我正在使用NH 3.2.0

1

交換

QueryOver.Of<ContestPlanningGSItemDAO>(() => contestPlanningGSItemAlias) 
    .Where(() => contestPlanningGSItemAlias.GameSystem.Id == gameSystemAlias.Id) 
    .Select(c => c.GameSystem)) 

QueryOver.Of<ContestPlanningGSItemDAO>() 
    .Where(x => x.GameSystem == gameSystemAlias)) 
+0

以這種方式,NHibernate不會再拋出異常,但會產生錯誤的sql,尤其是在它正在生成where條件的子查詢中:'SELECT this_0_.GameSystem as y0_ FROM ContestPlanningGSItemDAO this_0_ WHERE this_0_.GameSystem is null' instead與外部查詢結合使用 – Marto 2012-07-12 10:21:17