开发者

Calling Stored Procedure using NHibernate as a Named Query with Input Parameters

开发者 https://www.devze.com 2023-04-12 12:11 出处:网络
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 runni

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 D开发者_运维知识库etails:

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

Calling Stored Procedure using NHibernate as a Named Query with Input Parameters

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号