views:

163

answers:

0

Hi, I need help in creating the correct fluent nh mapping for this kind of scenario:

A category can be a child of one or more categories. Thus, resulting to this entity:

public class Category : Entity, IAggregateRoot
{
    [EntitySignature]
    public virtual string Name { get; set; }
    public virtual IList<Category> Parents { get; set; }
    public virtual IList<Category> Children { get; set; }
    public virtual IList<ProductCategory> Products { get; set; }

    public Category()
    {
        Parents = new List<Category>();
        Children = new List<Category>();
        Products = new List<ProductCategory>();

    }

    public virtual void AddCategoryAsParent(Category parent)
    {
        if (parent != this && !parent.Parents.Contains(this) && !Parents.Contains(parent))
        {
            Parents.Add(parent);
            parent.AddCategoryAsChild(this);
        }
    }

    public virtual void RemoveCategoryAsParent(Category parent)
    {
        if (Parents.Contains(parent))
        {
            Parents.Remove(parent);
            parent.RemoveCategoryAsChild(this);
        }
    }

    public virtual void AddCategoryAsChild(Category child)
    {
        if(child != this && !child.Children.Contains(this) && !Children.Contains(child))
        {
            Children.Add(child);
            child.AddCategoryAsParent(this);
        }
    }

    public virtual void RemoveCategoryAsChild(Category child)
    {
        if(Children.Contains(child))
        {
            Children.Remove(child);
            child.RemoveCategoryAsParent(this);
        }
    }
}

My initial mapping is this:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Id(p => p.Id).GeneratedBy.Identity();

        HasManyToMany(x => x.Parents)
            .Table("CategoryParents")
            .ParentKeyColumn("CategoryId")
            .ChildKeyColumn("ParentCategoryId")
            .Cascade.SaveUpdate()
            .LazyLoad()
            .AsBag();

        HasManyToMany(x => x.Children)
            .Table("CategoryParents")
            .ParentKeyColumn("ParentCategoryId")
            .ChildKeyColumn("CategoryId")
            .Cascade.SaveUpdate()
            .Inverse()
            .LazyLoad()
            .AsBag();
    }
}

The problem with this mapping is whenever I remove a category as a parent or as a child of another category, the resulting SQL statement is this:

NHibernate: DELETE FROM CategoryParents WHERE CategoryId = @p0;@p0 = 2
NHibernate: INSERT INTO CategoryParents (CategoryId, ParentCategoryId) VALUES (@p0, @p1);@p0 = 2, @p1 = 3

It deletes all the mapping first, then insert the remaining mapping. The proper way is just delete the category parent mapping which this kind of statement:

DELETE FROM CategoryParents WHERE CategoryId = @p0 AND ParentCategoryId = @p1;@p0 = 2, @p1=1

Any ideas?