Mapping multi level inheritance with subclasses - Conventions are lost

283 views
Skip to first unread message

ZeusTheTrueGod

unread,
Jul 6, 2009, 9:02:49 AM7/6/09
to Fluent NHibernate
Hi
I have this hierarchy of classes

<code>
public class P
{
public int Id{get;set;}
}
public class C1:P{}
public abstract class C2:P{}
public class C21:C2
{
public C22 Parent { get; set; }
}
public class C22:C2
{
public List<C21> Items { get; set; }
}
</code>

It looks like conventions are not applied when I map this with
SubClass
<code>
public class Map:ClassMap<P>
{
public Map()
{
Id(x => x.Id);
DiscriminateSubClassesOnColumn("disc")
.SubClass<C1>("c1", MapC1)
.SubClass<C2>("c2", MapC2)
;
}

private void MapC1(SubClassPart<C1> mapping)
{

}

private void MapC2(SubClassPart<C2> mapping)
{
mapping.SubClass<C21>("c21", MapC21);
mapping.SubClass<C22>("c22", MapC22);
}

private void MapC21(SubClassPart<C21> mapping)
{
mapping.References(x => x.Parent);//!Convention on
DefaultForeignKey is Lost here
}
private void MapC22(SubClassPart<C22> mapping)
{
mapping.HasMany(x => x.Items);
}
}
</code>

Column name of Many-to-One is not set because convention is not
applied

Here is the reason:
DiscriminatorPart.cs line 38
mapping.ParentClass.AddSubclass(subclass.GetSubclassMapping());
classMap.AddSubclass(subclass); // HACK for conventions

SubclassPart.cs line 109
mapping.AddSubclass(subclass.GetSubclassMapping());

When I use a mapping for children conventions are applied with the
hack, but for children of children
no conventions are applied because SubclassPart does not applies
conventions recursively on it own subclasses

Part of xml mapping result -
<xml>
<subclass name="DomainMappingGenerator.SimpleTestFixture+C21,
DomainMappingGenerator, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" discriminator-value="c21">
<many-to-one name="Parent" column="" />
</subclass>
</xml>

I solved my problem with explicitly seting of foreign key in
mapping.References(x => x.Parent).Column("aaa")







ZeusTheTrueGod

unread,
Jul 6, 2009, 9:11:18 AM7/6/09
to Fluent NHibernate
and here is a testcase (sorry that I don't test xml automatically -
still do not know how to get a xmlmapping for classmap)
http://code.google.com/p/fluent-nhibernate/issues/detail?id=270
attachment contains models,mapping and code for exporting mapping to
hbm

ZeusTheTrueGod

unread,
Jul 6, 2009, 10:09:52 AM7/6/09
to Fluent NHibernate
(Changed a subject back to original)
Reply all
Reply to author
Forward
0 new messages