views:

56

answers:

1

Hi Guys, I have a very simple scenario, using NHibernate:

  • one abstract base class "animal";
  • two concrete subclass "cat" and "dog" with discriminator column (1 for dog and 2 for cat);
  • one normal class Person;

Person have a many-to-one of animal.

I want retrieve a list of person with a dog. How can I do this? thx a lot

A: 

If I understand well, something like this :

var list = session.CreateCriteria(typeof(Person))
    .CreateCriteria("Animal")
     .Add(Expression.Eq("discriminatorField", 1))
    .List<Person>();

above is "Criteria API" but you can use "HQL" it's more something like this :

StringBuilder query = new StringBuilder();
query.Append("from Person pers where ");
query.Append("from Animal ani ... and :wichAnimal");
query.Append("and cus.IsActive = :wichAnimal");

IList<Person> list = session
    .CreateQuery(query.ToString())
    .SetInt16("wichAnimal",1)
    .List<Person>();

HTH

Kris-I