nHibernate - 一个只包含超类型的集合 [英] nHibernate- a Collection which would contain only a supertype

查看:117
本文介绍了nHibernate - 一个只包含超类型的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有以下类:


$ p $ class Employee
{
public string Name {get;组; }
}

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

class Company
{
public IList< Person> CurrentEmployees {get;组; Employee和HistoricalEmployee是使用table-per-class-map来映射的,所以我们可以使用table-per-class-平等战略。

当我检索CurrentEmployees集合时,我只希望它包含Employee的元素,而不是HistoricalEmployees。当员工死亡时,它们不是HistoricalEmployees。但是他们成为了HistoricalEmployee(还有一些其他的属性,比如终止日期等)。
显然,历史雇员的数量将随着时间的增加而超过员工数量,
所以我不能获取所有HistoricalEmployees,当我只需要当前雇员。



如何(流利)配置集合只检索超类的元素?

我认为这与Polymorphism属性有关,但我无法弄清楚如何做到这一点。





谢谢,

Jhonny

解决方案

好的,我这样做了:

  mapping.HasMany(x => x.CurrentEmployees)
//.其中(pqa => pqa。 TimeOut!= nul l)
。Where(TerminationDate is null)

显然, )函数在属性上创建一个过滤器,这正是我所需要的。

注意到我使用了字符串版本,并且注释了Func<>版本。

这是因为目前(FNH 1.1),据我所知,Func<>版本不起作用。

希望这可以帮助某人,

J



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

解决方案

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

这篇关于nHibernate - 一个只包含超类型的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆