开发者

NHibernate Search with inheritance

I have a Client class like that:

public clas开发者_运维知识库s 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...


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜