gpt4 book ai didi

c# - Nhibernate - 创建 SQLQuery - IndexOutOfRangeException

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:45 25 4
gpt4 key购买 nike

我们有一个名为 Customer 的表,它具有三个子类/表,即。 Cust1、Cust2 和 Cust3 由 hbm 使用连接子类链接。

所有 4 个表都以 CustomerId 作为主键。客户表中的一条记录将在 Cust1 或 Cust 2 或 Cust3 表中有一条记录。

使用标准 Nhibernate List() 获取列表可以正确获取类及其子类。

但是,为了优化我们的查询,我们不得不使用 CreateSQlQuery 方法。

在goolging上,我们发现获取类及其子类的正确方法是有一个选择查询,例如

var sqlQuery = Session.CreateSqlQuery(
select C.*,
case if C1.CustId is not null then 1
else if C2.CustId is not null then 2
....
from Customer C
left join Cust1 C1 on C1.CustId = C1.CustId
left join Cust2 C2 on C2.CustId

where C.CustID in (x,y,z,blah,blah).).

sqlQuery.AdddEntity("C",typeof(Customer));

sqlQuery.List();

当 Nhibernate 在内部生成查询时,需要更改 case 和 alais 以区分 4 个表之间的 CUstId 列,否则会抛出 clazz 错误..

在运行查询时,我们得到 Nhibernate 异常为

"IndexOutOfRangeException - Duration"

Cust1(子类)表有一个名为 Duration 的列。我将表列重命名为 Duration_BE 以检查列名是否是问题,,然后它抛出了错误

"IndexOutOfRangeException - Duration-BE"

这种工作模式的引用是.. http://www.methodicmadness.com/2009/01/nhibernate-what-is-heck-clazz.html

谁能帮帮我。

最佳答案

确保您选择了所有表格中的所有字段。

例如

var sqlQuery = Session.CreateSqlQuery(@"
select
C.*,
C1.*,
C2.*, -- You need all of the fields from the joined tables
case
if C1.CustId is not null then 1
else if C2.CustId is not null then 2
end as clazz_
from
Customer C
left join Cust1 C1 on C.CustId = C1.CustId
left join Cust2 C2 on C.CustId = C2.CustId
where
C.CustID in (x,y,z)"
);

关于c# - Nhibernate - 创建 SQLQuery - IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220775/

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