NHibernate的连接顺序

时间:2015-04-14 00:30:06

标签: c# nhibernate right-join

我遇到了Nhibernate的问题 - 它将右连接置于其他连接的中间。这是我的代码:

private DetachedCriteria GetBaseCriteria()
{
    return DetachedCriteria.For<Foo>("foo")
        .CreateAlias("foo.Bar", "bar")
        .CreateAlias("bar.Baz", "baz", JoinType.InnerJoin);
}

public void Method()
{
    var criteria = GetBaseCriteria();

    criteria.CreateAlias("foo.Something", "something", JoinType.RightOuterJoin);

    ...
}

结果SQL是

SELECT *
FROM foo
INNER JOIN bar ON ...
RIGHT OUTER JOIN something s ON ...
INNER JOIN baz ON ...       
WHERE ...

不幸的是,该查询的结果与所需的

不同
SELECT *
FROM foo
INNER JOIN bar ON ...
INNER JOIN baz ON ...
RIGHT OUTER JOIN something s ON ...     
WHERE ...

我试图改变GetBaseCriteria中别名的顺序 - 但没有运气......

1 个答案:

答案 0 :(得分:0)

就我而言,我可以通过简单地重新安排***。hbm.xml文件中定义的关系来改变NHibernate形成的查询的顺序。

例如如果foo.hbm.xml定义为

<class name="***, ***" table="foo" >
...
<many-to-one name="bar">...</many-to-one>
<many-to-one name="something">...</many-to-one>
<many-to-one name="baz" >...</many-to-one>

只需更改bar,something和baz的顺序如下

<class name="***, ***" table="foo" >
...
<many-to-one name="bar">...</many-to-one>
<many-to-one name="baz" >...</many-to-one>
<many-to-one name="something">...</many-to-one>