views:

46

answers:

1

Hi,

I'm having a problem getting FluentNHibernate's HasMany<> mapping to behave as I would expect.

I have a class hierarchy of Child is a descendant of Parent, defined using table-per-hierarchy, discriminated by the class name in a column named 'TYPE'. An unrelated class Group contains a collection of Child elements.

So, the Group class is defined as:

public class Group
{
    public virtual IList<Child> Children {get; protected set;}

    public Group()
    {
        Children = new List<Children>();
    }
}

My map in Fluent NHibernate looks like this:

public class GroupMap : SubclassMap<Group>
{
    public GroupMap()
    {
        HasMany<Child>(x => x.Children)
            .Not.LazyLoad()
            .Inverse()
            .Cascade.All();
    }
}

I would expect that using Group.Children would result in a collection of objects of type Child, but it is returning a collection that contains objects of either type Parent or Child.

Looking at the SQL generated, the select statement is not differentiating based on the TYPE column in the table that holds the Parent and Child objects.

Changing my mapping to:

public class GroupMap : SubclassMap<Group>
{
    public GroupMap()
    {
        HasMany<Child>(x => x.Children)
            .Inverse()
            .Not.LazyLoad()
            .Where("Type = 'Child'")
            .Cascade.All();
    }
}

solves the problem, but it seems like a hack to me. Shouldn't the declaration "HasMany' infer that the results should be only of type Child?

Am I mapping my collections incorrectly?

A: 

The short answer is I've moved away from using FluentNHibernate mappings.

I'm back to using plain .hbm.xml mappings and much happier as a result.

David Montgomery

related questions