Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate entity HasMany collections of different subclass types

So everything is working well with the basic discriminator mapping. I can interact directly with entities A and B without any problems.

public class BaseType {}
public class EntityA : BaseType {}
public class EntityB : BaseType {}

This maps without drama in the BaseType mapping as

DiscriminateSubClassesOnColumn<string>("Type")
               .SubClass<BaseType>("A", m => { })
               .SubClass<BaseType>("B", m => { });

The problem occurs when: in an aggregate we want to map collections to each subclass

Using mapping like below

public class AggregateMap: BaseMap<Aggregate>
{
        public AggregateMap()
        {
                HasMany<EntityA>(x => x.ACollection).AsSet().Cascade.All();
                HasMany<EntityB>(x => x.BCollection).AsSet().Cascade.All();            
        }
}

These obviously arent full mappings but are the minimum amount to descibe what I am attempting. Items added to ACollection and BCollection are persisted correctly through the cascading when Aggregate is saved. However, when aggregate is retrieved there is confusion on the type discrimination.

I have run through so many different possible solutions I no longer know what didn't work. I feel that I shouldn't have to provide a where clause on the collections but things just aren't working for me.

Any clues would be appreciated.

like image 200
berko Avatar asked Jun 12 '09 06:06

berko


1 Answers

You mapping looks odd, in particular I think it should look more like this

DiscriminateSubClassesOnColumn<string>("Type")
               .SubClass<EntityA>("A", m => { })
               .SubClass<EntityB>("B", m => { });

Having said that it seems that method is depreciated and you should instead define the following (taken from Automapping Subclasses:

public class ParentMap : ClassMap<Parent>
{
  public ParentMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);

    DiscriminateSubClassesOnColumn("type");
  }
}

public class ChildMap : SubclassMap<Child>
{
  public ChildMap()
  {
    Map(x => x.AnotherProperty);
  }
} 

Not sure this will fix it though, I am yet to encounter your scenario.

Edit: The issue is also raised here, sounding more like a bug to me

like image 118
Iain Avatar answered Nov 09 '22 17:11

Iain