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:
以下是这些类及其在流畅的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
谢谢Alexey Zakharov
2 个解决方案
#1
0
if you choose to set the Cascade parameter at NONE, it's up to you to manage this relation and so to put the store property value in the product to null before deleting the store. Like Karl said often you come with another class (association class) to hold additionnal information about the relation.
如果选择将Cascade参数设置为NONE,则由您来管理此关系,因此在删除商店之前将产品中的store属性值设置为null。就像卡尔经常说的那样,你会来另一个类(关联类)来保存关于关系的额外信息。
#2
0
Taking Hibernate out of the mix and just thinking from the database perspective, it appears that your product entity keeps its own list of stores it is stocked in. I recommend separating out that information so you have an association entitity called product store that would associate a product with different stores. Example:
将Hibernate从混合中解放出来,只是从数据库的角度思考,看来您的产品实体保留了自己的库存商店列表。我建议将这些信息分离出来,这样您就拥有了一个名为product store的关联权限产品与不同的商店。例:
Product(name, id) Store(id, name, address)
产品(名称,ID)商店(ID,名称,地址)
ProductStore(product_id, Store_id, quantity_in_store....).
ProductStore(product_id,Store_id,quantity_in_store ....)。
This should address the kind of problem you describe.
这应该解决您描述的问题。
Keep business rules in mind: If a store is deleted, I suspect the products in the store need to be accounted for and so on . . .
记住业务规则:如果删除商店,我怀疑商店中的产品需要计算,等等。 。 。
K
ķ
#1
0
if you choose to set the Cascade parameter at NONE, it's up to you to manage this relation and so to put the store property value in the product to null before deleting the store. Like Karl said often you come with another class (association class) to hold additionnal information about the relation.
如果选择将Cascade参数设置为NONE,则由您来管理此关系,因此在删除商店之前将产品中的store属性值设置为null。就像卡尔经常说的那样,你会来另一个类(关联类)来保存关于关系的额外信息。
#2
0
Taking Hibernate out of the mix and just thinking from the database perspective, it appears that your product entity keeps its own list of stores it is stocked in. I recommend separating out that information so you have an association entitity called product store that would associate a product with different stores. Example:
将Hibernate从混合中解放出来,只是从数据库的角度思考,看来您的产品实体保留了自己的库存商店列表。我建议将这些信息分离出来,这样您就拥有了一个名为product store的关联权限产品与不同的商店。例:
Product(name, id) Store(id, name, address)
产品(名称,ID)商店(ID,名称,地址)
ProductStore(product_id, Store_id, quantity_in_store....).
ProductStore(product_id,Store_id,quantity_in_store ....)。
This should address the kind of problem you describe.
这应该解决您描述的问题。
Keep business rules in mind: If a store is deleted, I suspect the products in the store need to be accounted for and so on . . .
记住业务规则:如果删除商店,我怀疑商店中的产品需要计算,等等。 。 。
K
ķ