开发者

NHibernate CreateCriteria and CreateQuery generates different sql?

开发者 https://www.devze.com 2023-01-02 17:38 出处:网络
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'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")
            .SetPara开发者_如何学Cmeter("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


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'...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号