Problem Description:

I am trying to execute a stored procedure with an input parameter. The stored procedure executes correctly when run from MSSQL 2008 SQL Studio. However I get an error while running it as a named query using NHibernate.

I get the error at the time of SessionFactory configuration. Hence I believe that I am not mapping the named query correctly.

Stored Procedure Details:

Name of the Procedure : CASCADE_POSITIONTEMPLATE_PERMISSIONS
Input Parameter : PositionTemplateId

The stored procedure returns a count which I want to return encapsulated in the PositionTemplateUpdateCascadeResult class which has the specified property.

Named Query/Class Mapping:

Named Query Mapping:

    <hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2" 
assembly="StudentVoiceGroups.Entities" namespace="StudentVoiceGroups.Entities" >
      <sql-query name="CASCADE_POSITIONTEMPLATE_PERMISSIONS" cacheable="false">
        <return class="PositionTemplateUpdateCascadeResult" alias="result">
          <return-property name="UpdatedPositionsCount">
            <return-column name="UpdatedPositionsCount" />
          </return-property>
        </return>
        exec CASCADE_POSITIONTEMPLATE_PERMISSIONS :PositionTemplateId
      </sql-query>
    </hibernate-mapping>

Following is the class which I want to be returned:

public class PositionTemplateUpdateCascadeResult
{
    public int UpdatedPositionsCount { get; set; }
}

when I execute the procedure in MSSQL Studio as:

   EXEC [CASCADE_POSITIONTEMPLATE_PERMISSIONS]  15

I get the following

enter image description here

Let me know if any thing else is needed.


EDIT: I was able to make this work when I removed the return class: Following mapping works correctly:

<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2" 
    assembly="StudentVoiceGroups.Entities" namespace="StudentVoiceGroups.Entities" >
<sql-query name="CASCADE_POSITIONTEMPLATE_PERMISSIONS" cacheable="false">            
      exec CASCADE_POSITIONTEMPLATE_PERMISSIONS :PositionTemplateId
</sql-query>
</hibernate-mapping>

Should the return class be an entity which is mapped to a table? In my case it is a simple class. I was of the opinion that the return class is just used like we use ResultTransformer.

有帮助吗?

解决方案

You may not have defined the query correctly in the HBM file. Try this:

<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2"
  assembly="StudentVoiceGroups.Entities" namespace="StudentVoiceGroups.Entities" >
  <sql-query name="CASCADE_POSITIONTEMPLATE_PERMISSIONS" cacheable="false">
    <return alias="result" class="YourNamespace.PositionTemplateUpdateCascadeResult, YourNamspaceAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <return-property name="UpdatedPositionsCount" column="UpdatedPositionsCount" />
    </return>
    exec CASCADE_POSITIONTEMPLATE_PERMISSIONS @PositionTemplateId=?
  </sql-query>
</hibernate-mapping>

It should work because I used something similar in my answer to this SO question.

EDIT: for the return element, make sure you have a fully qualified return type (this includes the fully qualified assembly name with the correct version number).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top