nHibernate - Populate a mapped function with namedQuery

时光总嘲笑我的痴心妄想 提交于 2019-12-12 01:02:17

问题


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.

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

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.

Is this possible?

Map

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 + ")");
    }
}

Repository

// 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>

回答1:


You should not put the formula in the fluent mapping, this because you have it in your namedquery:

 Map(x => x.ResponseCount).Formula("(SELECT COUNT(*) FROM QuoteResponse WHERE QuoteResponse.QuoteId = Quote.Id and QuoteResponse.Status = " + (int)QuoteResponseRepository.Status.Live + ")");

Just map the field as your do with any other field and you will be fine:

Map(x => x.ResponseCount);



回答2:


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?

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.



来源:https://stackoverflow.com/questions/15635058/nhibernate-populate-a-mapped-function-with-namedquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!