views:

222

answers:

1

I'm new to NHibernate and can't figure out why these two statements generates different sql.

the first one only get the ClientInformation (with Information and Client being Proxies) which is what i want.

return repository
            .CreateQuery("from ClientInformation ci where ci.Information.IsMandatory = true and ci.Client.Id = :clientId")
            .SetParameter("clientId", clientId)
            .List<ClientInformation>();

The second one generates everything. All data is returned for the 3 entities, which is not what i want

return repository.CreateCriteria()
            .CreateAlias("Information", "inf")
            .CreateAlias("Client", "cli")
            .Add(Expression.Eq("cli.Id", clientId))
            .Add(Expression.Eq("inf.IsMandatory", true))
            .List<ClientInformation>();

What i'm i doing wrong ? thanks

+1  A: 

Actually it all boils down to what you want to do. First of all the Criteria queries honor the mapping definitions (lazy/eager joins etc) where in constrast HQL queries unless defined otherwise everything is lazy (excluding value properties of course)

Secondly the CreateAlias method defines which entities to join and default behaviour is to also select them.

Note that you are calling

repository.CreateCriteria()

and if that wraps directly to nhSession.CreateCriteria() then you haven't defined exactly what you want to select. So, try to make this

nhSession.CreateCriteria(typeof(ClientInformation));

which will be translated as 'select only ClientInformation'...

Jaguar
mappings are defined as lazyload. and createCriteria returns a typeof(ClientInformation)
mathieu tanguay