【问题标题】:Can I use table-valued function as query source in NHibernate?我可以在 NHibernate 中使用表值函数作为查询源吗?
【发布时间】:2011-05-14 02:07:01
【问题描述】:

亲爱的社区,我有一个问题要问您,您可能已经猜到了。所以。 我希望 NHibernate 根据对表值 sql 函数的评估来过滤查询结果。 NHibernate 生成的可能的 SQL 查询可能类似于以下内容:

SELECT
   [whatever]
FROM
   [whatever]
   INNER JOIN dbo.FnMyTableValuedFunction() as MyAlias ON
       [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey
       AND
       [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey

也可以这样写:

SELECT
    [whatever]
FROM
    [whatever]
WHERE
    EXISTS(
        SELECT
            1
        FROM
            dbo.FnMyTableValuedFunction() AS MyAlias
        WHERE
            [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey
            AND
            [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey
    )

我想使用 Criteria API 生成这样的查询。 据我所知,没有办法告诉 NHibernate 它应该加入什么以及如何加入。因此,可能存在的一种解决方案是第二种解决方案。
不幸的是,我没有幸运地发现如何使用表值函数作为相关子查询的查询源。你能帮我解决这个问题吗?

【问题讨论】:

    标签: nhibernate criteria-api user-defined-functions correlated-subquery


    【解决方案1】:

    您可以将自定义方法添加到派生的 SQLDialect 中,并在条件中使用该方法,查看 NHibernate.Dialect.Dialect 中的RegisterFunction,所有方言都继承。

    【讨论】:

      【解决方案2】:

      除了 SQL(HQL、Criteria、Linq、QueryOver)之外的所有 NHibernate 查询方法都适用于实体,而不是表或任何其他数据库工件。

      因此,如果您使用 Criteria,则需要将 FnMyTableValuedFunction 映射到实体,或将 SQLCriterion 用于任意 SQL 块。

      对于后者,是的,EXISTS 可能是要走的路。您可以将整个条件(包括 EXISTS)包含在 SQLCriterion 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多