NHibernate GetNamedQuery无法执行存储过程

时间:2013-05-04 06:50:20

标签: nhibernate

所以我有一个名为get_by_cat_and_tag的存储过程,它应该返回表MS,并且在我的C#代码中它应该返回MS个对象的列表,其中MS.hbm.xml包含以下内容:

<sql-query name="get_by_cat_and_tag">
    <!-- return type must be an NHibernate mapped entity -->
    <return class="MS">

      <return-property column="Id" name="Id" />
      <return-property column="FileName" name="FileName" />
      <return-property column="DisplayTitle" name="DisplayTitle" />
      <return-property column="IsEnabled" name="IsEnabled" />
      <return-property column="MediaType" name="MediaType" />
      <return-property column="Priority" name="Priority" />
      <return-property column="DateCreated" name="DateCreated" />
      <return-property column="DateLastModified" name="DateLastModified" />
      <return-property column="Description" name="Description" />
      <return-property column="ImagePath" name="ImagePath" />

    </return>
    <!--exec get_by_cat_and_tag@categoryId=:catId, @tagId=:tId-->
    exec get_by_cat_and_tag:catId, :tId
</sql-query>

从这个存储过程返回的所有列名都是正确的,为了方便起见,我使它们变得相同。

数据访问层包含对存储过程的调用:

var query = session.GetNamedQuery("get_by_cat_and_tag")
                   .SetParameter("catId", categoryId).SetParameter("tId", tagId)
                   .List<MS>();

异常消息:

could not execute query

[ exec get_by_cat_and_tag @p0, @p1 ]
Name:catId - Value:130  Name:tId - Value:449
[SQL: exec get_by_cat_and_tag @p0, @p1]

The stack trace:
   at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
   at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name)
   at NHibernate.Loader.Loader.GetInstanceClass(IDataReader rs, Int32 i, ILoadable persister, Object id, ISessionImplementor session)
   at NHibernate.Loader.Loader.InstanceNotYetLoaded(IDataReader dr, Int32 i, ILoadable persister, EntityKey key, LockMode lockMode, String rowIdAlias, EntityKey optionalObjectKey, Object optionalObject, IList hydratedObjects, ISessionImplementor session)
   at NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session)
   at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您在字标记后面缺少空格: -

exec get_by_cat_and_tag:catId, :tId

应该是: -

exec get_by_cat_and_tag :catId, :tId

另请注意,返回的所有列也必须匹配以防万一,它始终值得检查一个。