You are using inheritance in your class structure, to properly instrcut nHibernate to store the base class and sub class properties, you'll have to redefine your mappings and maybe the objects a little bit
Basically you should use SubClassMap
instead of ClassMap
for all sub classes and only define new properties in those sub class mappings.
I've added some code for the elements (best guess according to your diagram)
public abstract class CombatElement
{
public CombatElement()
{
Elements = new List<CombatElement>();
}
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<CombatElement> Elements { get; set; }
public virtual void Add(CombatElement element)
{
Elements.Add(element);
}
}
public class Formation : CombatElement
{
}
public class Soldier : CombatElement
{
public virtual int Rank { get; set; }
}
And the new mapping would look like this:
public class CombatElementMap : ClassMap<CombatElement>
{
public CombatElementMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name).Not.Nullable().Length(100);
HasMany(x => x.Elements)
.AsBag()
.Fetch.Join()
.Cascade.AllDeleteOrphan();
}
}
public class FormationMap : SubclassMap<Formation>
{
public FormationMap()
{
//Id(x => x.Id).GeneratedBy.GuidComb();
}
}
public class SoldierMap : SubclassMap<Soldier>
{
public SoldierMap()
{
//Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Rank);
}
}
also make sure you call .Flush
after saving your entities, otherwise it might not get stored in your database.
session.Save(fe);
session.Flush();