2009-08-22 95 views
0

我得類產品和商店具有多對多關係刪除操作問題,在多對多關係

我想店裏的刪除不會導致刪除相關產品的 而且產品沒有的刪除的導致相關商店的刪除。

由於外鍵約束,當前正在刪除實體引起的異常。

下面是這個類及其流利的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(); 
    } 
} 

感謝, 阿列克謝·扎哈羅夫

回答

0

如果你選擇設置爲NONE級聯參數,它是由你來管理這個關係,所以要刪除存儲之前把商店屬性值在產品零。就像卡爾經常說的,你會與另一個階級(聯合階級)一起來獲得關於關係的額外信息。

0

以休眠出來混的,並從數據庫 角度來看只是在想,看來你產品實體保留其自己的庫存清單 。我建議分離出該信息 ,以便您擁有一個稱爲產品庫的關聯實體,該關聯實體將 關聯產品wi不同的商店。例如:

產品(姓名,身份證) 存儲(ID,姓名,地址)

ProductStore(產品,商店編號,quantity_in_store ....)。

這應該解決您描述的那種問題。

請記住商業規則:如果商店被刪除,我懷疑商店裏的 產品需要進行覈算等等。 。 。

ķ