功能NHibernate:多对多自引用映射功能、NHibernate

2023-09-06 09:06:23 作者:欲握玫瑰↘必承其痛

我需要在创建正确流利NH映射为这种情况的帮助:

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

使用此映射的问题是每当予删除类别作为父或作为其它类别的子,将得到的SQL语句是这样的:

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

任何想法?

推荐答案

看着你的映射,我觉得你是想改变你的梯级选项之后。这是一篇文章,概述父 - 实体之间的亲子关系。虽然它来自来看具有孤立的实体的时候,我想你会发现博客的帮助。祝你好运...

after looking at your mappings I think you are wanting to change your Cascade options. Here is an article which outlines a parent - child relationship between entities. Although it comes from the point of view of having orphaned entities I think you will find the blog helpful. Good luck...

http://ayende.com/Blog/archive/2006/12/02/nhibernatecascadesthedifferentbetweenallalldeleteorphansandsaveupdate.aspx