Marto
Marto

Reputation: 31

NHibernate where not exists with QueryOver

I have an entity GameSystemDAO and an entity ContestPlanningGSItemDAO with a property GameSystem which is a many-to-one of type GameSystemDAO. What is a QueryOver expression which corresponds to the following SQL?

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

I tried the following (and many other variations):

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();

but always get a KeyNotFoundException: The given key was not present in the dictionary. It seems like NHibernate is looking for a property named gameSystemAlias on the ContestPlanningGSItemDAO instance.

What am I doing wrong?

Upvotes: 3

Views: 1935

Answers (2)

Marto
Marto

Reputation: 31

I found that updating to NHibernate 3.3.1 gets this to work; I was using NH 3.2.0

Upvotes: 0

Firo
Firo

Reputation: 30813

exchange

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

with

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

Upvotes: 1

Related Questions