我正在尝试使用 nHibernate 将存储过程的输出映射到我的项目中的对象。
该对象的定义如下:
public class NewContentSearchResult
{
public string Name { get; set; }
public string ContentType { get; set; }
public int Count { get; set; }
public int CMIId { get; set; }
public int FeatureId { get; set; }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
映射如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Class.Assembly"
namespace="Class.Assembly"
default-lazy="false" default-cascade="none">
<class name="NewContentSearchResult" mutable="false" check="none">
<composite-id unsaved-value="none">
<key-property name="CMIId" type="int" />
<key-property name="FeatureId" type="int" />
</composite-id>
<property name="ContentType" type="string" />
<property name="Name" type="string" />
<property name="Count" type="int" />
</class>
<sql-query name="spWebGetNewContentBySalesRole">
<return class="NewContentSearchResult" lock-mode="read">
<return-property name="Name" column="Name" />
<return-property name="ContentType" column="FeatureDesc" />
<return-property name="Count" column="Number" />
<return-property name="CMIId" column="CMIId" />
<return-property name="FeatureId" column="FeatureId" />
</return>
exec core.spWebGetNewContentBySalesRole :SalesRoleId
</sql-query>
</hibernate-mapping>
存储的 proc 调用是正确的,我得到的结果类似于 SalesRoleId 266(例如):
CMIId FeatureDesc FeatureId Name Count
4000719 Guest Book 12 Charlie Brown 2
4000719 Audio Guest Book 3 Charlie Brown 1
如果没有复合键(仅使用 CMIId),它工作得很好,除非有 2 个结果(如上)共享一个 CMIId ......第二个被第一个覆盖。
我必须使用复合键,CMIId/FeatureId 是逻辑组合。
当我现在运行它时,我得到一个异常:
NHibernate.ADOException: could not execute query
[ exec core.spWebGetNewContentBySalesRole ? ]
Name: SalesRoleId - Value: 266
[SQL: exec core.spWebGetNewContentBySalesRole ?] ---> System.IndexOutOfRangeException: CMIId22_0_.