I have a tree stucture model (used composite pattern).
it's class diagram is like this:
database diagram:
and sample of tree:
the problem rise when I want to persist CombatElement tree which its depth is more than one, when I try to persist such object, NHibernate only save the objects which are in the 1st level and ignores the objects which connected to 2nd level object and so:
if I create this tree :
CombatElement fe = new Formation() { Name = "Alpha Company" };
fe.Add(new Soldier()
{
Name = "Joe",
Rank = 1
});
fe.Add(new Soldier()
{
Name = "Jack",
Rank = 2
});
CombatElement platoon =
new Formation();
platoon.Name = "1st Platoon";
fe.Add(platoon);
platoon.Add(
new Soldier()
{
Name = "Adam",
Rank = 2
});
platoon.Add(
new Soldier()
{
Name = "Arthur",
Rank = 3
});
only "Joe", "1st Platoon" and "Jack" will be saved into the database and "Arthur" and "Adam" which are the subelemnts of 1st Platoon will be ignored and won't be saved!!
here is mapping classes:
public class CombatElementMap:ClassMap<CombatElement>
{
public CombatElementMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name).Not.Nullable().Length(100);
}
}
///////////////////////////
public class FormationMap:ClassMap<Formation>
{
public FormationMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
HasMany(x => x.Elements).Cascade.AllDeleteOrphan();
}
}
///////////////////////////
public class SoldierMap:ClassMap<Soldier>
{
public SoldierMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Rank);
}
}
I have cascaded the Formation objects, but the problem is still persist. why this happens? It just confusing me!!