【问题标题】:NHibernate where not exists with QueryOverQueryOver 不存在的 NHibernate
【发布时间】:2012-07-11 16:54:10
【问题描述】:

我有一个实体 GameSystemDAO 和一个实体 ContestPlanningGSItemDAO,其属性 GameSystem 是 GameSystemDAO 类型的多对一。什么是 QueryOver 表达式对应下面的 SQL?

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 的属性。

我做错了什么?

【问题讨论】:

  • 一个完整的堆栈跟踪通常很有帮助,所以请总是从头开始发布它
  • @Firo 异常被抛出 NHibernate.Persister.Entity.AbstractPropertyMapping.ToType(string propertyName),第 37 行(使用 NHibernate 3.2.0)
  • 感谢您的发帖,这让我继续前进。

标签: nhibernate queryover


【解决方案1】:

交换

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

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

【讨论】:

  • 这样,NHibernate 不再抛出异常,而是生成错误的 sql,特别是在子查询中,它正在生成这样的 where 条件:SELECT this_0_.GameSystem as y0_ FROM ContestPlanningGSItemDAO this_0_ WHERE this_0_.GameSystem is null 而不是将其与外部查询连接起来
【解决方案2】:

我发现更新到 NHibernate 3.3.1 可以让它工作;我使用的是 NH 3.2.0

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多