gpt4 book ai didi

NHibernate:存储过程和组件

转载 作者:行者123 更新时间:2023-12-04 00:21:23 26 4
gpt4 key购买 nike

我正在调用存储过程作为 NamedQuery,它正在运行美好的。当我尝试加载组件时,问题就开始了。存储过程返回这样的记录集

[IDEmployee, EmployeeName, City, Country ]

IDEmployee 和 EmployeeName 属于映射实体,City 和 Country 来自组件

这是映射:

<class name="Employee" table="employees"> 
<id name="IDEmployee">
<column name="idemployee" />
<generator class="native" />
</id>
<property name="EmployeeName ">
<column name="employeename" />
</property>
<component name="Address" class="Address">
<property name="City">
<column name="city" />
</property>
<property name="Country">
<column name="country" />
</property>
</component>
</class>

这是我用来调用存储过程的代码:

  <sql-query name="GetEmployeesByCompany"> 
<return class="Employee">
<return-property column="idemployee" name="IDEmployee" />
<return-property column="employeename" name="EmployeeName" />
<return-property column="city" name="City" />
<return-property column="country" name="Country" />
</return>
EXEC GetEmployeeByCompany:idcompany
</sql-query>

这里是我用来调用存储的代码:

 var result = 
session<ISession>().GetNamedQuery("GetEmployeesByCompany")
.SetInt32("idcompany", companyId)
.List<Employee>().ToList();

这是我在运行我的应用程序时遇到的错误

System.IndexOutOfRangeException: t1_282_0_ 
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name)
in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs:line 236
at NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(IDataReader rs, Object id, Object obj, ILoadable rootLoadable, String[][] suffixedPropertyColumns, Boolean allProperties, ISessionImplementor session)
in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs:line 2508
at NHibernate.Loader.Loader.LoadFromResultSet(IDataReader rs, Int32 i, Object obj, String instanceClass, EntityKey key, String rowIdAlias, LockMode lockMode, ILoadable rootPersister, ISessionImplementor session)
in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 991

如果我从 map 中删除该组件,一切正常。我还尝试了一些带有嵌套返回列的返回属性,但没有成功。

有什么建议吗?谢谢!

最佳答案

我最近不得不面对这个问题,这就是我的出路。

<sql-query name="GetEmployeesByCompany">
<return class="Employee">
<return-property column="idemployee" name="IDEmployee" />
<return-property column="employeename" name="EmployeeName" />
<return-property name="Address"> <!-- name of the component -->
<return-column name="city" /> <!-- name of the resultset field -->
<return-column name="country" /> <!-- name of the resultset field -->
</return-property>
</return>
EXEC GetEmployeeByCompany:idcompany

您对返回属性中的字段使用的顺序非常重要,它必须与结果集中的顺序相匹配。周围没有任何文档,我希望这可以帮助你!

关于NHibernate:存储过程和组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7169929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com