简体   繁体   English

流利的NHibernate子类分页具有多种类型

[英]Fluent NHibernate subclass paging with more than one type

I'm about to embark on a rather complicated mapping strategy and I have a sticking point that I'm not sure what the best track is... 我将着手一项相当复杂的制图策略,但我有一个棘手的问题,即我不确定最好的轨道是什么...

Basically, I have a table per subclass schema with four subclasses. 基本上,每个子类架构都有一个带有四个子类的表。 I'm using a standard repository pattern around NHibernate (for now) that this might not fit neatly into, which uses the LINQ provider (part of why I might change it later for caching and lazy loading stuff support from my mappings). 我正在使用围绕NHibernate的标准存储库模式(目前),该模式可能不太适合使用LINQ提供程序(这是为什么以后可能会更改它以便从映射中缓存和延迟加载东西支持的部分原因)。

My requirement is that I must pull a list of TWO DIFFERENT subclasses at one time, so that I can implement paging over the query set. 我的要求是我必须一次提取两个不同子类的列表,以便可以在查询集上实现分页。 I am not sure how to do that... 我不确定该怎么做...

I was thinking something along the lines of 我在想一些类似的事情

Session.Query<Superclass>().Where(x => x is SubClass1 || x is SubClass2).Skip().Take();

But I'm unclear how that would translate, or if there is a better way? 但是我不清楚这将如何翻译,或者是否有更好的方法?

Actually your example query is exactly how you would do it. 实际上,您的示例查询正是您要执行的操作。 Of course it depends on if you map your super and sub classes correctly with one ClassMap and several SubclassMaps (in fluent nhibernate). 当然,这取决于您是否使用一个ClassMap和几个SubclassMap(正确地使用了nhibernate)正确地映射了上级和子类。

You can query different sub types by using the is statement 您可以使用is语句查询不同的子类型

var result = session.Query<LifeForm>().Where(t => t is Cat || t is Programmer).Skip(5).Take(10).ToList();

You can even do queries where you filter on sub type specific properties doing something like this: 您甚至可以在查询中对子类型特定的属性进行过滤,如下所示:

var result2 = session.Query<LifeForm>()
    .Where(t => t is Cat || t is Programmer)
    .Where(p=>((p as Cat).Cuteness > 5) ||((p as Programmer).IsSenior == true)).ToList();

The downside of this is that nhibernate creates enormous queries left outer joining all sub types regardless of which sub types you are using and filters the results by heavily using case statements on the PK . 不利之处在于,nhibernate会在连接所有子类型之前创建大量查询,而不管您使用的是哪种子类型,并且通过在PK上大量使用case语句来过滤结果。

...
where 
case when csclifefor0_3_.Id is not null then 3 
when csclifefor0_2_.Id is not null then 2 
when csclifefor0_4_.Id is not null then 4 
when csclifefor0_5_.Id is not null then 5 
when csclifefor0_9_.Id is not null then 9 
when csclifefor0_10_.Id is not null then 10 
...

This might not be an issue though. 不过,这可能不是问题。 The generated SQL statements just get more complex the more sub classes you might have... 您可能拥有的子类越多,生成的SQL语句就会变得越复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM