NHibernate 搜索与继承
我有一个这样的客户端类:
public class Client
{
public Person Pers { get; set; }
}
我有 2 个 Person 的子类:
public class PersonType1 : Person
{
protected string att1;
protected string att2;
}
public class PersonType2 : Person
{
protected string att3;
protected string att4;
}
public class Person
{
protected string attx;
protected string atty;
}
所以,我的客户端可能是 PersonType1 或 PersonType2...
我需要执行客户端搜索...该搜索的参数是 att1、att2、att3 ,att4,attx,atty...但所有这些都是可选的...
我正在尝试使用 ICriteria 执行该搜索,但我不知道如何指定该继承方案...
I have a Client class like that:
public class Client
{
public Person Pers { get; set; }
}
And I have 2 Person´s child class :
public class PersonType1 : Person
{
protected string att1;
protected string att2;
}
public class PersonType2 : Person
{
protected string att3;
protected string att4;
}
public class Person
{
protected string attx;
protected string atty;
}
So, my Client could be PersonType1 or PersonType2...
I need execute a Client search ... The parameters to that search are att1,att2,att3,att4,attx,atty... But all those are optional...
I´m trying to execute that search with ICriteria, but I dont know how specify that inheritance scheme...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实不能这样做,因为在标准级别上,Person 类与任何派生类无关。结果集将是一个
IList
,即使迭代该集合将显示元素也将包含 PersonType1 和 PersonType2 类型(假设我们只是从数据库中获取整个集合而无需任何限制)。也就是说,您可以通过解决方法达到预期的效果:
将每个派生类型定义为 N 个派生类型的新子查询
,然后在主 Criteria 查询上做
交替操作,ISQLQuery 没有这样的限制。
编辑我在下面添加必要的调整以找到客户
我将把条件重写为
条件,并表达对连接的s
的子查询限制。You really cannot do this because at the criteria level the Person class is agnostic of any derived classes. The result set will be an
IList<Person>
even though iterating through the collection will show you that the elements will also comprise of PersonType1 and PersonType2 types (assume that we just fetch the whole set from the DB without any restrictions).That said, you can achieve the desired effect with a workaround:
Define each derived type as a new subquery
for N derived types and then on your main Criteria query just do
alternatively, an ISQLQuery has not such limitations.
EDIT i'm adding below the necessary adjustments to find Client
i will re-write the criteria to be a
<Client>
criteria and express the subquery restriction on the joined<Person>s
.