NHibernate - populate data from multiple stored procedures
Yeah, yet another nhibernate &开发者_JS百科 stored procedures question, sorry...
I have two stored procedures which return the same structure (so I would like to bind them to the same class), the only difference is the 'where' condition in the second stored procedure. Can I bind both stored procedures to my class? How would the mapping file look like? Or would you prefer different approaches:
- use only one stored procedure and add new input parameter which says how the procedure should behave
- don't use NHibernate at all - use DataAdapter instead
- create a view (but in my example I don't know how...)
- ...
First stored procedure:
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
Second stored procedure:
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
And this is my class:
public class CheckedBadCodeOverview
{
public virtual long Number { get; set; }
public virtual int NumberOfScans { get; set; }
public CheckedBadCodeOverview()
{
}
}
Just add a <sql-query>
for each SP.
Check 16.2.2. Using stored procedures for querying
精彩评论