NHibernate - 从多个存储过程填充数据

时间:2011-01-20 20:38:26

标签: nhibernate stored-procedures nhibernate-mapping

是的,还有另一个nhibernate&存储过程问题,对不起......

我有两个存储过程返回相同的结构(所以我想将它们绑定到同一个类),唯一的区别是第二个存储过程中的'where'条件。我可以将两个存储过程绑定到我的类吗?映射文件如何?或者您更喜欢不同的方法:

  • 仅使用一个存储过程并添加新的输入参数,该参数说明过程的行为方式
  • 根本不使用NHibernate - 而是使用DataAdapter
  • 创建一个视图(但在我的例子中我不知道如何......)
  • ...

第一个存储过程:

ALTER procedure [BadCodes].[GetCodesBetween]
    @codeFrom bigint,
    @codeTo bigint
as
begin
    set nocount on

    ;with BadCodesOriginal as (
        select Code 
        from Scanning.BadCodes 
        where Code between @codeFrom and @codeTo
    ),
    BadCodesScanned as (
        select Code, count(*) NumberOfBadCodesScanned 
        from BadCodes.Scan
        where Code between @codeFrom and @codeTo
        group by Code
    )
    select t1.Code Code, isnull(t2.NumberOfBadCodesScanned, 0) NumberOfBadCodesScanned
    from BadCodesOriginal t1 left join BadCodesScanned t2 on (t1.Code = t2.Code)
    order by t1.Code
end

第二个存储过程:

ALTER procedure [BadCodes].[GetCodesBetweenDifference]
    @codeFrom bigint,
    @codeTo bigint
as
begin
    set nocount on

    ;with BadCodesOriginal as (
        select Code 
        from Scanning.BadCodes 
        where Code between @codeFrom and @codeTo
    ),
    BadCodesScanned as (
        select Code, count(*) NumberOfBadCodesScanned 
        from BadCodes.Scan
        where Code between @codeFrom and @codeTo
        group by Code
    )
    select t1.Code Code, isnull(t2.NumberOfBadCodesScanned, 0) NumberOfBadCodesScanned
    from BadCodesOriginal t1 left join BadCodesScanned t2 on (t1.Code = t2.Code)
    where isnull(t2.NumberOfBadCodesScanned, 0) <> 1
    order by t1.Code
end

这是我的班级:

public class CheckedBadCodeOverview
    {
        public virtual long Number { get; set; }
        public virtual int NumberOfScans { get; set; }

        public CheckedBadCodeOverview()
        {
        }
    }

1 个答案:

答案 0 :(得分:2)

只需为每个SP添加<sql-query>

检查16.2.2. Using stored procedures for querying