【问题标题】:Why does Linq to Nhibernate produce an outer join为什么 Linq to Nhibernate 会产生外连接
【发布时间】:2011-03-19 01:29:06
【问题描述】:

我在 Linq to Nhibernate 产生带有外部连接的查询时遇到问题。

例如:

return Session.Linq<ClientContact>().Where(c => c.Client.Id = 13).ToList();

产生类似于以下内容的查询:

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

注意外部连接 this.clientid = client1.clientid(+)。嘘!

为了解决这个问题,我重新使用了 Session.CreateCriteria。

return Session.CreateCriteria(typeof (ClientContact)).Add(Restrictions.Eq("Client.Id", clientId)).List<ClientContact>();

产生以下查询:

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

注意没有外连接 this.clientid = client1.clientid。耶!

有人知道为什么会这样吗?

【问题讨论】:

    标签: nhibernate oracle9i linq-to-nhibernate


    【解决方案1】:

    至少对于 SQL Server,使用 Fluent NH 与 not-null="true"Not.Nullable() 的多对一映射会导致 Get 操作使用内连接而不是左连接。尝试将其添加到 ClientContact 中的 Client 映射。

    【讨论】:

    • 我试过了。我们正在使用 Oracle 数据库并使用 Fluent NH。我的 ClientContact 映射类有以下参考: References(x => x.Client).Column("clientid").Not.Nullable().LazyLoad();
    • 那么这可能是 LINQ to NHibernate 中的错误或设计。
    • 我很确定 Jamie 是正确的……(当前)linq 提供者只做左外连接。
    • 查看查询计划。在 sql server 2008 上,当有一个 where 子句测试右侧表的列时,我对“join”和“left join”查询版本都有相同的查询计划。
    猜你喜欢
    • 2011-09-20
    • 2011-11-13
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多