更新:现已解决,请参阅下面的答案。
我在尝试为MSSQL存储过程编写NHibernate映射文件(.hbm.xml)的正确方法时遇到了一些麻烦。
存储过程接受两个参数并返回包含多个具有整数值的列的单行结果集。
我的映射文件如下:
<?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>
我也尝试了以下内容,正如某人在论坛上所建议的那样,另一个人与存储过程有关的类似问题......
<?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>
两者似乎都不起作用......我只是得到以下错误:
的类型初始值设定项 'TestApp.DataAccess.Sql.NHibernateHelper' 抛出异常。
{“命名查询中的错误: {MI_MonthlyInstructionCount}“}
我看不到它是存储过程(尽管错误会暗示它是......),因为它似乎在MSSQL Server Manager中成功运行。
非常感谢任何帮助!干杯!
答案 0 :(得分:3)
已解决:我现在已解决此问题。首先,ID属性的问题被设置为“name = Id”而不是“name = Month”。其次,我必须将exec命令包装在CDATA中,并在参数之间放置一个逗号分隔符。下面是完整的工作映射文件,以供将来参考任何遇到类似问题的人使用。
<?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>
干杯!