正确的NHibernate的映射存储过程? [英] Correct NHibernate Mapping For Stored Procedure?

查看:109
本文介绍了正确的NHibernate的映射存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是现在解决了,见下面的答案

我有一点试图找出编写存储过程MSSQL的NHibernate的映射文件(的.hbm.xml)的正确方法麻烦了。

I am having a bit of trouble trying to work out the correct way to write an NHibernate Mapping File (.hbm.xml) for a MSSQL Stored Procedure.

存储过程接受两个参数,并返回包含具有整数值几列单行结果集。

The Stored Procedure accepts two parameters and returns a single row result set containing several columns which have integer values.

我的映射文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TestApp.DataAccess"
                   namespace="TestApp.DataAccess.Models">

  <class name="MonthlyInstructionCount" lazy="true">
    <id name="Id" column="Month">
      <generator class="native" />
    </id>

    <property name="Month" />
    <property name="MapRequests" />
    <property name="Instructions" />
    <property name="DrainsLookSee" />
    <property name="DrainsFullCctv" />
    <property name="Soils" />
    <property name="Roots"/>
    <property name="Arb" />

    <loader query-ref="MI_MonthlyInstructionCount"/>
  </class>

  <sql-query name="MI_MonthlyInstructionCount">
    <return class ="MonthlyInstructionCount">
      <return-property name="Month" column="Month" />
      <return-property name="MapRequests" column="MapRequests" />
      <return-property name="Instructions" column="Instructions" />
      <return-property name="DrainsLookSee" column="DrainsLookSee" />
      <return-property name="DrainsFullCctv" column="DrainsFullCctv" />
      <return-property name="Soils" column="Soils" />
      <return-property name="Roots" column="Roots" />
      <return-property name="Arb" column="Arb" />
    </return>
    exec dbo.MI_MonthlyInstructionCount :StartDate :NumberOfMonths
  </sql-query>

</hibernate-mapping>

我也曾尝试以下,作为一个论坛上建议由专人为另一个人是具有与存储过程类似的问题...

I have also tried the following, as suggested by someone on a forum for a similar problem another person was having relating to Stored Procedures...

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TestApp.DataAccess"
                   namespace="TestApp.DataAccess.Models">

  <sql-query name="MI_MonthlyInstructionCount">
    <return class ="MonthlyInstructionCount" />
    exec dbo.MI_MonthlyInstructionCount :StartDate :NumberOfMonths
  </sql-query>

</hibernate-mapping>

既不似乎工作...我只是得到以下错误:

Neither of which seem to work... I just get the following error:

有关的类型初始值
  TestApp.DataAccess.Sql.NHibernateHelper
  抛出异常。

The type initializer for 'TestApp.DataAccess.Sql.NHibernateHelper' threw an exception.

{在命名查询错误:
  {MI_MonthlyInstructionCount}

{"Errors in named queries: {MI_MonthlyInstructionCount}"}

我看不出它是存储过程(虽然该错误会建议它是...),因为它似乎在MSSQL服务器管理器成功运行。

I can't see it being the Stored Procedure (though the error would suggest it is...) as it seems to run successfully in MSSQL Server Manager.

任何帮助很多AP preciated!干杯!

Any help is much appreciated! Cheers!

推荐答案

解决:我现在已经解决了这个问题。首先,有ID属性的问题被设置为name = ID,而不是NAME =月。其次,我不得不在CDATA包裹exec命令,并把参数之间用逗号分隔符。完整的工作映射文件低于以供将来参考任何人遇到了类似的问题。

RESOLVED: I have now resolved this issue. First of all there was the issue of the ID property being set to "name=Id" instead of "name=Month". Secondly, I had to wrap the exec command in a CDATA and also put a comma seperator between the parameters. The full working mapping file is below for future reference to anyone experiencing a similar problem.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TestApp.DataAccess"
                   namespace="TestApp.DataAccess.Models">

  <class name="MonthlyInstructionCount" lazy="true">
    <id name="Month">
      <generator class="native" />
    </id>

    <property name="MapRequests" />
    <property name="Instructions" />
    <property name="DrainsLookSee" />
    <property name="DrainsFullCctv" />
    <property name="Soils" />
    <property name="Roots"/>
    <property name="Arb" />

    <loader query-ref="MI_MonthlyInstructionCount"/>
  </class>

  <sql-query name="MI_MonthlyInstructionCount">
    <return class="MonthlyInstructionCount">
      <return-property name="Month" column="Month" />
      <return-property name="MapRequests" column="MapRequests" />
      <return-property name="Instructions" column="Instructions" />
      <return-property name="DrainsLookSee" column="DrainsLookSee" />
      <return-property name="DrainsFullCctv" column="DrainsFullCctv" />
      <return-property name="Soils" column="Soils" />
      <return-property name="Roots" column="Roots" />
      <return-property name="Arb" column="Arb" />
    </return>
    <![CDATA[ 
    exec MI_MonthlyInstructionCount :StartDate, :NumberOfMonths
    ]]>
  </sql-query>

</hibernate-mapping>

干杯!

这篇关于正确的NHibernate的映射存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆