Nhibernate 连接过滤

发布于 2024-11-18 05:04:51 字数 2407 浏览 3 评论 0原文

我有一个关于 NHIBERNATE 中连接的问题。我们的 sql 查询有一个问题,该查询是由 nhibernate 生成的。我们的数据库开发人员优化了原始 sql,使其能够按照我们的需要工作,但是我们需要更改 nhibernate 代码以使生成的 sql 看起来像是经过优化的。

查询的原始部分的部分是:

FROM   PERSON_VISIT this_
                     inner join PERSON_Basic per2_
                       on this_.PERSON_ID = per2_.PERSON_ID
                     left outer join PERSONC_QUESTIONS perint10_
                       on per2_.PERSON_ID = perint10_.PERSON_ID
                     left outer join TELEPHONE_QUESTIONS intaudit13_
                       on perint10_.PP_QUESTIONS_ID = intaudit13_.PP_QUESTIONS_ID
                     inner join C_QUESTIONS intdef14_
                       on perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and perint10_.QUESTIONS_CODE = intdef14_.QUESTIONS_CODE
                          and perint10_.QUESTION_ID = intdef14_.QUESTION_ID

优化的部分是:

FROM   PERSON_VISIT this_
                     inner join PERSON_Basic per2_
                       on this_.PERSON_ID = per2_.PERSON_ID
                     left outer join PERSONC_QUESTIONS perint10_
                       on per2_.PERSON_ID = perint10_.PERSON_ID
                     left outer join TELEPHONE_QUESTIONS intaudit13_
                       on perint10_.PP_QUESTIONS_ID = intaudit13_.PP_QUESTIONS_ID
                     left outer join C_QUESTIONS intdef14_
                       on perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and perint10_.QUESTIONS_CODE = intdef14_.QUESTIONS_CODE
                          and perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

将查询从内联接更改为左外联接很容易,我只更改了一行代码:

        .CreateAlias("PersonInt.QuestionEntity", "IntDef", JoinType.LeftOuterJoin)

但是如何

                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

使用 nhibernate 代码添加?

有一个选项可以将 PERSON_VISIT 定义的引用添加到 C_QUESTIONS,但问题是 PERSON_VISIT 随处使用,我不希望此更改可能破坏其他查询,我只想添加一行代码来添加,我该怎么做?有什么方法可以访问原始连接来更改它吗?或者其他方式将其添加

                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

到查询中? 我知道有人会说我们可以通过 criteria.Add 对查询添加限制,但这不是一个选项,因为数据库开发人员优化了我们的查询,将此限制从 WHERE 子句转移到联接。

如何在不更改模型定义的情况下快速做到这一点?只更改这一个查询而不更改整个模型?

I have a question about joins in NHIBERNATE. We had an issue with our sql query that was generated but nhibernate. Our db developer optimized the raw sql so it works as we need, but we need to change the nhibernate code to make generated sql look like optimized.

the part of the original part of the query is:

FROM   PERSON_VISIT this_
                     inner join PERSON_Basic per2_
                       on this_.PERSON_ID = per2_.PERSON_ID
                     left outer join PERSONC_QUESTIONS perint10_
                       on per2_.PERSON_ID = perint10_.PERSON_ID
                     left outer join TELEPHONE_QUESTIONS intaudit13_
                       on perint10_.PP_QUESTIONS_ID = intaudit13_.PP_QUESTIONS_ID
                     inner join C_QUESTIONS intdef14_
                       on perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and perint10_.QUESTIONS_CODE = intdef14_.QUESTIONS_CODE
                          and perint10_.QUESTION_ID = intdef14_.QUESTION_ID

The optimized one is :

FROM   PERSON_VISIT this_
                     inner join PERSON_Basic per2_
                       on this_.PERSON_ID = per2_.PERSON_ID
                     left outer join PERSONC_QUESTIONS perint10_
                       on per2_.PERSON_ID = perint10_.PERSON_ID
                     left outer join TELEPHONE_QUESTIONS intaudit13_
                       on perint10_.PP_QUESTIONS_ID = intaudit13_.PP_QUESTIONS_ID
                     left outer join C_QUESTIONS intdef14_
                       on perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and perint10_.QUESTIONS_CODE = intdef14_.QUESTIONS_CODE
                          and perint10_.QUESTION_ID = intdef14_.QUESTION_ID
                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

To change query from inner join to left outer join is easy, i changed only one line of code:

        .CreateAlias("PersonInt.QuestionEntity", "IntDef", JoinType.LeftOuterJoin)

But how I can add

                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

using nhibernate code?

There is an option to add reference from PERSON_VISIT definition to C_QUESTIONS, but the problem is that
PERSON_VISIT is used everywhere and I don't want this change to possibly break other queries, I just wnat to add only one line of code to add, how I can do that? Is there any way to have access to the raw join to change it? Or some other way to add this

                          and intdef14_.DISCIPLINE_CODE = this_.DISCIPLINE_CODE

To the query?
I know that somebody will say that we can add a restriction to the query through criteria.Add, but it is not an option cause db developer optimized our query taking this restriction from WHERE clause to the join.

How I can do that quickly without changing the models definitions? Just changing only this one query without changing the whole model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-11-25 05:04:52

可以使用 HQL 和 Criteria API。

这个问题为您提供了答案:向外连接添加条件使用 nhibernate

这样的东西可能会解决你的问题。

.CreateAlias("PersonInt.QuestionEntity", "IntDef", JoinType.LeftOuterJoin, 
      Restrictions.EqProperty("DISCIPLINE_CODE", "IntDef.DISCIPLINE_CODE"))

It is possible using HQL and the Criteria API's.

This question gives you the answer: Adding conditionals to outer joins with nhibernate

Something like this may solve your issue.

.CreateAlias("PersonInt.QuestionEntity", "IntDef", JoinType.LeftOuterJoin, 
      Restrictions.EqProperty("DISCIPLINE_CODE", "IntDef.DISCIPLINE_CODE"))
夜血缘 2024-11-25 05:04:52

感谢您的回答。我们在项目中使用 2.0 版本的 NHibernate,因此我们没有机会在有限制的情况下使用 .CreateAlias 的新方法。

我已经使用拦截器解决了一个问题:

public class SqlInterceptor : EmptyInterceptor, IInterceptor
{
    SqlString IInterceptor.OnPrepareStatement(SqlString sql)
    {
        //manipulating with the sql

        return sql;
    }
}

        var factory = Session.SessionFactory;
        var session = factory.OpenSession(new SqlInterceptor());

并使用我的查询而不进行更改。

Thanks for answers. We use 2.0 version of NHibernate in our project so we didn't have a chance to use new methods of .CreateAlias with restrictions.

I have fixed an issue using Interceptors:

public class SqlInterceptor : EmptyInterceptor, IInterceptor
{
    SqlString IInterceptor.OnPrepareStatement(SqlString sql)
    {
        //manipulating with the sql

        return sql;
    }
}

than

        var factory = Session.SessionFactory;
        var session = factory.OpenSession(new SqlInterceptor());

And use my query without a change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文