Question

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.

Was it helpful?

Solution

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>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top