NHibernate CreateCriteria 和 CreateQuery 生成不同的sql?
我是 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>();
我做错了什么? 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,这一切都归结为你想做什么。
首先,Criteria 查询遵循映射定义(惰性/热切连接等),与 HQL 查询相反,除非另有定义,否则一切都是惰性的(当然不包括值属性)
其次,CreateAlias 方法定义要连接的实体,默认行为也是选择它们。
请注意,您正在调用
,如果它直接包装到 nhSession.CreateCriteria(),那么您还没有准确定义您想要选择的内容。
因此,尝试将其
翻译为“仅选择 ClientInformation”...
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
and if that wraps directly to nhSession.CreateCriteria() then you haven't defined exactly what you want to select.
So, try to make this
which will be translated as 'select only ClientInformation'...