Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where clause with type check "is" operator does not use discriminator

I have mapped a class hierarchy in NHibernate. Simple as that:

class abstract Animal
class Dog : Animal
class Cat: Animal
class Cow: Animal

In mapping I have discriminator values set on ANIMAL_TYPE column:

Dog -> dog
Cat -> cat
Cow -> cow

All kind of queries work. Except this one, when I need to fetch objects of two particular types. I wrote is like this:

QueryOver.Of<Animal>().Where(animal => animal is Dog || theme is Cat)

And I receive no items in result. When I look into generated query, NHibernate generates:

(this_.ANIMAL_TYPE = @p0 or this_.ANIMAL_TYPE = @p1)

which is fine, but the values in @p0 and @p1 parameters contain full class name, eg.

Zoo.Model.Cat

instead of discriminator values. How can I solve it? Do I have to keep my discriminator values in sync with type names? I would like to avoid it if possible.

like image 937
dragonfly Avatar asked Nov 28 '25 06:11

dragonfly


1 Answers

This question & answer should be working for you: How can QueryOver be used to filter for a specific class? (asked just a day before you)

var query = session.QueryOver<Animal>()
    .Where(animal => animal.GetType() == typeof (Dog) 
                  || animal.GetType() == typeof (Cat)
          );

var list = query.List<Animal>();
like image 185
Radim Köhler Avatar answered Nov 30 '25 00:11

Radim Köhler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!