2010-01-13 7 views
2

je besoin d'aide dans la création de la cartographie nh correcte couramment pour ce genre de scénario:NHibernate: ManyToMany mapping auto-référencement

Une catégorie peut être un enfant d'une ou plusieurs catégories. Ainsi, ce qui à cette entité:

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); 
     } 
    } 
} 

Ma cartographie initiale est la suivante:

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(); 
    } 
} 

Le problème avec cette cartographie est à chaque fois que je supprime une catégorie en tant que parent ou un enfant d'une autre catégorie, la L'instruction SQL résultante est la suivante:

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

Il supprime tout le mappage en premier, puis insère le mappage restant. La bonne façon est juste de supprimer la mise en correspondance parent de la catégorie qui ce genre de déclaration:

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

Des idées?

+1

je remarquai que vous utilisez IAggregateRoot. Cela ne va-t-il pas à l'encontre de plus d'un parent? – Nathan

Répondre