简体   繁体   中英

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:

.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.

Digging a bit deeper I found that the query is translated internally to something similar to WHERE x.class=animal in HQL. Is this a bug in Linq to NHibernate? Or is it expected behavior?

I fixed this bug added this feature to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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