简体   繁体   English

NHibernate Linq Query Where(x => x is Base Class) 没有得到派生对象

[英]NHibernate Linq Query Where(x => x is Base Class) doesn't get derived objects

Let's say I have the following structure, mapped with a discriminator (Table per hierarchy):假设我有以下结构,用鉴别器映射(每个层次结构的表):

    Entity (abstract, no discriminator)
      |
    Animal (abstract, no discriminator)
    /    \
Dog (1)  Cat(2)

If I query on this using Linq to NHibernate:如果我使用 Linq to NHibernate 对此进行查询:

.Where(x => x.Entity is Animal)

I get no results.我没有结果。 When looking at the generated query I expected to see:在查看生成的查询时,我希望看到:

where type in (1, 2)

But instead I got this:但是我得到了这个:

where type='animal'

Animal is abstract and doesn't even have a discriminator, so the generated query is meaningless. Animal 是抽象的,甚至没有鉴别器,因此生成的查询毫无意义。

Digging a bit deeper I found that the query is translated internally to something similar to WHERE x.class=animal in HQL.深入挖掘一下,我发现查询在内部被转换为类似于 HQL 中的WHERE x.class=animal的内容。 Is this a bug in Linq to NHibernate?这是 Linq to NHibernate 中的错误吗? Or is it expected behavior?或者这是预期的行为?

I fixed this bug added this feature to NHibernate.修复了这个错误,将此功能添加到 NHibernate。 The changes are here and can be built from source.更改在这里并且可以从源代码构建。 I issued a pull request so hopefully this will be working soon.我发出了一个拉取请求,所以希望这将很快起作用。

Update : It was merged , and should be available in the next release.更新:它已合并,应该在下一个版本中可用。

Do you get expected result when using使用时是否得到预期的结果

session.Query<Animal>().Select(a => a.yourXEntity).ToList()

Or或者

session.Query<Animal>().SelectMany(a => a.yourXEntityCollection).ToList()

instead?反而? Those one should handle polymorphism.那些应该处理多态性。

While your lambda x.Entity is Animal gets very likely translated to HQL x.Entity.class = Animal which does not handle polymorphism and takes only the discriminator value of specified class, which defaults to its class name.虽然您的 lambda x.Entity is Animal很可能被转换为HQL x.Entity.class = Animal ,它不处理多态性并且仅采用指定类的鉴别器值,该值默认为其类名。

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

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