views:

161

answers:

1

I have a class hierarchy mapped into one table. There is one superclass and 8 different sub classes. A lot of my queries needs to fetch e.g. 2 of the sub classes only for a specific date. The table has a discriminator column that nhibernate itself uses. But when using LINQ for querying it is not possible to use this discriminator as there is no property for it.

Is there a commonly used trick for only fetching specific sub class when using nhibernate ?

For now I first have Linq 4 Nhiberneate query that fetches all sub classes into a given period. And then uses Linq 4 objects to filter on the sub classes that I need.

Is it possible to expose the discriminator column of the table as a property and thereby be able to make a where clause on it ?

+2  A: 

In Hql querying subclasses is done by class, so you'd do

from subclass 
where subclass.DateTime = :myDateTime

The docs also say you can query hierarchies by the special class property, eg:

from Eg.Cat cat where cat.class = Eg.DomesticCat

I don't know if this is possible with the Criteria API or NH Linq Provider.

You could always get all the instances with the correct time, and then filter client side, eg:

var allCandidates = from super in session.Linq<SuperClass>()
    where super.Date > DateTime.Now.AddDays(-1)
    select super
var results = from candidate in allCandidates
    where candidate.GetType() == typeof(SubClass)
    select candidate

It's a bit nasty, and if the subset of classes you're querying is always the same, you might be better off inserting another class in the hierarchy and querying that.

David Kemp