nHibernate- a Collection which would contain only

2019-08-12 04:20发布

问题:


I have the following classes:

class Employee
{
    public string Name { get; set; }
}

class HistoricalEmployee : Employee
{
    public DateTime TerminationDate { get; set; }
}

class Company
{
    public IList<Person> CurrentEmployees { get; set; }
}


Employee and HistoricalEmployee are mapped using table-per-class-heirarchy strategy.
When I retrieve the CurrentEmployees collection, I want it only to contain elements that are Employee, and NOT HistoricalEmployees.
when an employee 'dies', they're not really deleted,
but they become HistoricalEmployee (with a few more attributes, such as termination date etc.).
Obviously, over time, the number of HistoricalEmployees will exceed the number of Employees by magnitudes,
so I can't fetch all HistoricalEmployees when I only need current Employees.

How can I (fluently) configure the collection to only retrieve elements of the super class?
I think it's something to do with the Polymorphism property, but I couldn't really figure out how to do that.


thanks,
Jhonny

回答1:

Ok, I did this like so:

mapping.HasMany(x => x.CurrentEmployees)
            //.Where(pqa => pqa.TimeOut != null)
            .Where("TerminationDate is null")

apparently, the .Where() function creates a filter on the property, which is exactly what I needed.
notice that I used the string version, and commented-out the Func<> version.
This is because that currently (FNH 1.1), as far as I could determine, the Func<> version doesn't work.
hopes this helps somebody,
J