2

I am using Fluent NHibernate with table per subclass inheritance mapping. I want to reference to a list of specific objects, but i can't figure out, how to restict the result to objects of one specific class.

class PetMap : ClassMap<Pet>
{
    public PetMap()
    {
        Id(c => c.ID).GeneratedBy.Identity();            
    }
}

class DogMap : ClassMap<Dog>
{
    public DogMap()
    {
       Mac(c => c.DogSpecificProperty);                                
    }
}

class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
       Mac(c => c.CatSpecificProperty);                                    
    }
}

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {  
        Id(c => c.ID).GeneratedBy.Identity();

        //this works fine
        HasMany(c => c.Pets);

        //this dosen't work, because the result contains dogs and cats
        //how can I tell NHibernate to only fetch dogs or cats?
        HasMany<Pet>(c => c.Cats);
        HasMany<Pet>(c => c.Dogs);
    }
}

class Pet
{ 
   int ID;
}
class Dog : Pet
{
   object DogSpecificProperty;
}
class Cat : Pet
{
   object CatSpecificProperty;
}
class Person
{
   int ID;
   IList<Pet> Pets;
   IList<Dog> Dogs;
   IList<Cat> Cats;
}

Can anyone help me? Please excuse my poor english.

1 Answer 1

1

I'm not an expert in Fluent NH, but it seems to me that your person map should look like this:

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {  
        Id(c => c.ID).GeneratedBy.Identity();

        //this works fine
        HasMany(c => c.Pets);

        //this dosen't work, because the result contains dogs and cats
        //how can I tell NHibernate to only fetch dogs or cats?
        HasMany<Cat>(c => c.Cats);
        HasMany<Dog>(c => c.Dogs);
    }
}

Because you're Person has a Dogs property which is an IList not IList, and conversely the same for Cats.

Sign up to request clarification or add additional context in comments.

2 Comments

<code> HasMany<Pet>(c => c.Cats) </code> This look a litle bit stupid, but i think it is correct, because the Pet table has a reference to the Person table. I get this solution from an other stack overflow question. It seems to me, that HasMany<Pet>(c => c.Cat) does the same as HasMany<Cat>(c => c.Cat).KeyColumn("PetID"). In both cases i get a result of cats and dogs.
@trichterwolke if you're saying that the Cat and Dog classes are actually coming from the Pet table in your DB, then I would think that you would say that in the CatMap and DogMap, not in the PersonMap. I believe that's how I did that in the past when using FNH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.