为什么 Linq to Nhibernate 会产生外连接
我在使用 Linq to Nhibernate 生成带有外连接的查询时遇到问题。
例如:
return Session.Linq<ClientContact>().Where(c => c.Client.Id = 13).ToList();
生成类似于以下内容的查询:
SELECT... 来自 mw_crafru.clientcontact this_, mw_crafru.client client1_, mw_crafru.relationshiptype 关系4_ 其中 this_.clientid = client1_.clientid(+) AND this_.relationshiptypeid = relationship4_.relationshiptypeid AND client1_.clientid = :p0;:p0 = 13
注意外部连接 this.clientid = client1.clientid(+)。嘘!
为了解决这个问题,我重新使用 Session.CreateCriteria。
return Session.CreateCriteria(typeof (ClientContact)).Add(Restrictions.Eq("Client.Id", clientId)).List<ClientContact>();
生成以下查询:
SELECT... 来自 mw_crafru.clientcontact this_, mw_crafru.client client1_, mw_crafru.relationshiptype 关系4_ 其中 this_.clientid = client1_.clientid AND this_.relationshiptypeid = relationship4_.relationshiptypeid AND client1_.clientid = :p0;:p0 = 13
注意没有外部连接 this.clientid = client1.clientid。耶!
有谁知道为什么会发生这种情况?
I have an issue with Linq to Nhibernate producing queries with outer joins.
For example:
return Session.Linq<ClientContact>().Where(c => c.Client.Id = 13).ToList();
Produces a query similar to:
SELECT...
FROM mw_crafru.clientcontact this_,
mw_crafru.client client1_,
mw_crafru.relationshiptype relationsh4_
WHERE this_.clientid = client1_.clientid(+)
AND this_.relationshiptypeid = relationsh4_.relationshiptypeid
AND client1_.clientid = :p0;:p0 = 13
Notice the outer join this.clientid = client1.clientid(+). Boo!
To resolve the issue I went back to using Session.CreateCriteria.
return Session.CreateCriteria(typeof (ClientContact)).Add(Restrictions.Eq("Client.Id", clientId)).List<ClientContact>();
Produces the following query:
SELECT...
FROM mw_crafru.clientcontact this_,
mw_crafru.client client1_,
mw_crafru.relationshiptype relationsh4_
WHERE this_.clientid = client1_.clientid
AND this_.relationshiptypeid = relationsh4_.relationshiptypeid
AND client1_.clientid = :p0;:p0 = 13
Notice no outer join this.clientid = client1.clientid. Yay!
Anyone know why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少对于 SQL Server,使用 Fluent NH 与
not-null="true"
或Not.Nullable()
进行多对一映射会导致 Get 操作使用内连接而不是左连接。尝试将其添加到 ClientContact 中的客户端映射中。With SQL Server at least, a many-to-one mapping with
not-null="true"
, orNot.Nullable()
using Fluent NH, causes Get operations to use an inner join instead of a left join. Try adding that to your mapping for Client in ClientContact.