Question

I have a domain model like this:

Class diagram

In the mapping, I am using a table-per-hierarchy structure where all LineItemOption subtypes are stored in a single table and a named "OptionType" is used as a discriminator. This column isn't mapped to a property and isn't visible to the domain.

// FluentNHibernate ClassMap for LineItemOption
Map(lio => lio.Description);
DiscriminateSubClassesOnColumn("OptionType");

// FluentNHibernate SubclassMap for ColorOption
DiscriminatorValue("C")

// FluentNHibernate SubclassMap for GenericOption
DiscriminatorValue("O")

I am using the QueryOver API to fetch a list of Orders that contain a LineItem with a LineItemOption of a specific type containing a specific description.

private void AddColorRestrictionToQuery(
    IQueryOver<Order, Order> query,
    string color)
{
    query.JoinQueryOver<LineItem>(order => order.LineItems)
         .JoinQueryOver<LineItemOption>(line => line.Options)
         .Where(opt => opt.Description.IsLike(color))
         .Where(opt => opt is ColorOption);     // See below
}

This results in NHibernate adding "WHERE OptionType = MyNamespace.Entities.ColorOption" to the query. Instead of using the discriminator value, it seems to be using the fully-qualified namespace+class name.

Why is NHibernate using the class name instead of its discriminator?

Was it helpful?

Solution

I believe you want:

query.JoinQueryOver<LineItem>(order => order.LineItems)
     .JoinQueryOver<LineItemOption>(line => line.Options)
     .Where(opt => opt.Description.IsLike(color))
     .Where(opt => opt.GetType() == typeof(ColorOption));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top