nHibernate- a Collection which would contain only a supertype

核能气质少年 提交于 2019-12-24 11:53:03

问题



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



来源:https://stackoverflow.com/questions/4540936/nhibernate-a-collection-which-would-contain-only-a-supertype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!