NHibernate 搜索与继承

发布于 2024-08-11 02:37:52 字数 603 浏览 6 评论 0原文

我有一个这样的客户端类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-08-18 02:37:52

您确实不能这样做,因为在标准级别上,Person 类与任何派生类无关。结果集将是一个 IList,即使迭代该集合将显示元素也将包含 PersonType1 和 PersonType2 类型(假设我们只是从数据库中获取整个集合而无需任何限制)。

也就是说,您可以通过解决方法达到预期的效果:
将每个派生类型定义为 N 个派生类型的新子查询

var dc1 = DetachedCriteria.For(typeof(PersonType1)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att1", "foo"));

var dc2 = DetachedCriteria.For(typeof(PersonType2)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att3", "bar"));

,然后在主 Criteria 查询上做

CreateCriteria(typeof(Person)).Add(Subqueries.PropertyIn("Id", dc1) || Subqueries.PropertyIn("Id", dc2));

交替操作,ISQLQuery 没有这样的限制。

编辑我在下面添加必要的调整以找到客户
我将把条件重写为 条件,并表达对连接的 s 的子查询限制。

var crit = ses.CreateCriteria(typeof(Client)).CreateCriteria("Person","per").Add(Subqueries.PropertyIn("per.Id", dc1) || Subqueries.PropertyIn("per.Id", dc2));

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

var dc1 = DetachedCriteria.For(typeof(PersonType1)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att1", "foo"));

var dc2 = DetachedCriteria.For(typeof(PersonType2)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att3", "bar"));

for N derived types and then on your main Criteria query just do

CreateCriteria(typeof(Person)).Add(Subqueries.PropertyIn("Id", dc1) || Subqueries.PropertyIn("Id", dc2));

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.

var crit = ses.CreateCriteria(typeof(Client)).CreateCriteria("Person","per").Add(Subqueries.PropertyIn("per.Id", dc1) || Subqueries.PropertyIn("per.Id", dc2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文