使用 NHibernate 作为带有输入参数的命名查询来调用存储过程
问题描述:
我正在尝试使用输入参数执行存储过程。从 MSSQL 2008 SQL Studio 运行时,存储过程可以正确执行。但是,在使用 NHibernate 将其作为命名查询运行时出现错误。
我在 SessionFactory 配置时收到错误。 因此我相信我没有正确映射命名查询。
存储过程详细信息:
过程名称:CASCADE_POSITIONTEMPLATE_PERMISSIONS
输入参数:PositionTemplateId
存储过程返回一个计数,我想将其返回封装在具有指定属性的PositionTemplateUpdateCascadeResult
类中。
命名查询/类映射:
命名查询映射:
<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>
以下是我想要返回的类:
public class PositionTemplateUpdateCascadeResult
{
public int UpdatedPositionsCount { get; set; }
}
当我在 MSSQL Studio 中执行该过程时:
EXEC [CASCADE_POSITIONTEMPLATE_PERMISSIONS] 15
我得到以下内容
如果还需要任何其他信息,请告诉我。
编辑:当我删除返回类时,我能够完成这项工作: 以下映射工作正常:
<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>
返回类应该是映射到表的实体吗?就我而言,这是一个简单的课程。我认为返回类的使用就像我们使用 ResultTransformer 一样。
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
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有在 HBM 文件中正确定义查询。试试这个:
它应该有效,因为我在答案中使用了类似的东西 这个问题。
编辑:对于 return 元素,确保您有一个完全限定的返回类型(这包括完全合格的装配具有正确版本号的名称)。
You may not have defined the query correctly in the HBM file. Try this:
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).