我们有一个名为Customer的表,它有三个子类/表,即。Cust1、Cust2和Cust3,它们使用连接子类通过hbm连接.
这4个表都以CustomerId作为主键。customer表中的记录将在Cust1、Cust 2或Cust3表中有一条记录。
使用标准的Nhibernate列表()获取列表,将正确地获取类及其子类。
然而,为了优化我们的查询,我们必须使用CreateSQlQuery方法。
在goolging中,我们发现获取类及其子类的正确方法是有一个select查询,例如
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在内部生成查询时,需要对4个表之间的CUstId列进行区分,需要用case和alais进行更改,否则会引发带有clazz的错误。
在运行查询时,我们得到Nhibernate异常
"IndexOutOfRangeException -持续时间“
Cust1 (子类)表有一个名为工期的列。我将表列重命名为Duration_BE,以检查列名是否有问题,然后抛出错误
"IndexOutOfRangeException -持续时间-被“
这种工作模式的参考资料是..。http://www.methodicmadness.com/2009/01/nhibernate-what-is-heck-clazz.html
有人能帮我吗。
发布于 2010-02-17 23:00:30
确保您正在从所有表中选择所有字段。
例如:
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)"
);https://stackoverflow.com/questions/2220775
复制相似问题