2

我是 NHibernate 的新手,无法弄清楚为什么这两个语句会生成不同的 sql。

第一个只得到ClientInformation我想要的(信息和客户端是代理)。

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

第二个生成一切。为 3 个实体返回所有数据,这不是我想要的

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

我在做什么错?谢谢

4

1 回答 1

2

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

于 2010-06-04T09:06:27.947 回答