Problem with delete operation in many to many relation

Posted by Alexey Zakharov on Stack Overflow See other posts from Stack Overflow or by Alexey Zakharov
Published on 2009-08-22T15:10:37Z Indexed on 2010/04/07 21:03 UTC
Read the original article Hit count: 577

Hi,

I've got to classes Product and Store which have many to many relation

I want deleting of store not to cause deleting of related product And deleting of product not to cause deleting of related store.

Currently deleting of entity cause exception due to Foreign Key constraint.

Here is this classes and their mapping in fluent hibernate:

public class Product
{
    public Product()
    {
        this.StoresStockedIn = new List<Store>();
    }


    public virtual string Name { get; set; }

    public virtual double Price { get; set; }

    public virtual long ProductID { get; set; }

    public virtual IList<Store> StoresStockedIn { get; set; }

}

public class Store
{
    public Store()
    {
        this.Products = new List<Product>();
        this.Staff = new List<Employee>();

    }

    public virtual string Name { get; set; }
    public virtual IList<Product> Products { get; set; }
    public virtual IList<Employee> Staff { get; set; }
    public virtual long StoreID { get; set; }
}

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        this.Id(x => x.ProductID);
        this.Map(x => x.Name);
        this.Map(x => x.Price);
        this.HasManyToMany(x => x.StoresStockedIn)
            .Cascade.None()
            .Table("StoreProduct");
    }
public class StoreMap : ClassMap<Store>
{
    public StoreMap()
    {
        this.Id(x => x.StoreID);
        this.Map(x => x.Name);
        this.HasManyToMany(x => x.Products)
            .Cascade.None()
            .Inverse()
            .Table("StoreProduct");
        this.HasMany(x => x.Staff)
            .Cascade.All()
            .Inverse();
    }
}

Thanks, Alexey Zakharov

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping