nHibernate-使用namedQuery填充映射函数 [英] nHibernate - Populate a mapped function with namedQuery
问题描述
我在数据层中使用Fluent nHibernate
,并且我有一个大部分通过nHibernate/LINQ
填充的类,但是在一些高级用法中,需要由存储过程填充.
I'm using Fluent nHibernate
for my data layer, and I have a class that is mostly populated through nHibernate/LINQ
but in a few advanced usages, needs to be populated by a stored procedure.
我遇到的问题是类映射包括一个公式.当我调用nHibernate/LINQ
函数时,基础变量将按预期方式填充;当我调用GetNamedQuery()
函数时,它将引发错误:
值不能为空.参数名称:字段名
The problem I have is the class mapping includes a Formula. When I call a nHibernate/LINQ
function, the underlying variable is populated as expected; when I call the GetNamedQuery()
function it throws an error:
Value cannot be null. Parameter name: fieldname
对于NamedQuery
,没有填充公式"字段是完全合乎逻辑的(显然,我希望在此处使用子查询,而不是为返回的每条记录运行SQL语句!),但是我希望能够从存储过程中填充公式"值-或至少在查询中不要抛出错误.
It's completely logical that for a NamedQuery
, the Formula field isn't populated (obviously I want a subquery here rather than a SQL statement run for every single record returned!), but I'd like to be able to populate the Formula value from the stored procedure - or at least the query not to throw an error.
这可能吗?
地图
public class QuoteMap : ClassMap<Quote>
{
public QuoteMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
...
Map(x => x.ResponseCount).Formula("(SELECT COUNT(*) FROM QuoteResponse WHERE QuoteResponse.QuoteId = Id and QuoteResponse.Status = " + (int)QuoteResponseRepository.Status.Live + ")");
}
}
存储库
// Works fine
public ICollection<Quote> GetAllByStatus(Status status)
{
using (ISession session = NHibernateHelper.OpenSession())
{
var quoteQuery = (from quote in session.Query<Quote>()
where quote.Status == (int)status
select quote).ToList();
return quoteQuery;
}
}
// Dies horribly
public ICollection<Quote> GetListPendingByCompany(Guid companyId, Status status)
{
using (ISession session = NHibernateHelper.OpenSession())
return session.GetNamedQuery("QuoteGetListPendingByCompany")
.SetGuid("Company_Id", companyId)
.SetInt32("QuoteStatus", (int)status)
.List<Quote>();
}
SQL
CREATE PROCEDURE [dbo].[QuoteGetListPendingByCompany]
@CompanyId uniqueidentifier,
@QuoteStatus int
AS
BEGIN
SET NOCOUNT ON;
SELECT
Quote.*,
(
SELECT
COUNT(*)
FROM QuoteResponse
WHERE QuoteResponse.QuoteId = Quote.Id
) AS ResponseCount -- Needs to populate what is currently a formula field
FROM Quote
-- ... code removed
END
StoredProcsMap.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="QMP.Data" namespace="QMP.Data.Model">
<sql-query name="QuoteGetListPendingByCompany" callable="true">
<return class="QMP.Data.Model.Quote, QMP.Data" />
<![CDATA[
exec dbo.QuoteGetListPendingByCompany @CompanyId=:Company_Id,@QuoteStatus=:QuoteStatus
]]>
</sql-query>
</hibernate-mapping>
推荐答案
好了,编辑之后,我现在可以看到您要执行的操作,但不确定是否可行,如果要使用FORMULA
列,则可以忽略该列调用SP的NamedQuery
?
Ok after your edit I can now see what you are trying to do, I am not sure its possible, you want to ignore the FORMULA
column if using a NamedQuery
that calls a SP?
如果指定ResultTransformer,会发生什么?
What happens if you specify a ResultTransformer?
return session.GetNamedQuery("QuoteGetListPendingByCompany")
.SetGuid("Company_Id", companyId)
.SetInt32("QuoteStatus", (int)status)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(Quote)))
.List<Quote>();
}
尽管我怀疑它仍会发声.
Although I suspect it will still barf.
这篇关于nHibernate-使用namedQuery填充映射函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!