[英]Nhibernate doesnt cascade delete
I have a very simple problem with nhibernate (I just started using it) 我有一个非常简单的问题与nhibernate(我刚开始使用它)
I have the following hbm mapping files : 我有以下hbm映射文件:
<class name="Customer" table="Customers" lazy="false">
<id name="Id" column="CustomerId">
<generator class="native">
</id>
<property name="Name" />
<property name="Picture" type="BinaryBlob" />
<bag name="Orders" cascade="all-delete-orphan" lazy="false">
<key column="CustomerId" />
<one-to-many class="Order" />
</bag>
</class>
<class name="Order" table="Orders" lazy="false">
<id name="Id" column="OrderId">
<generator class="native">
</id>
<property name="Name" />
<property name="Picture" type="BinaryBlob" />
<bag name="Products" cascade="all-delete-orphan" lazy="false"
<key column="OrderId" />
<one-to-many class="Product" />
</bag>
</class>
<class name="Product" table="Products" lazy="false">
<id name="Id" column="ProductId">
<generator class="native">
</id>
<property name="Name" />
<property name="Picture" type="BinaryBlob" />
<property name="ProductStr" />
</class>
Customer class has an int id, string name, byte[] picture and IList of Orders. Customer类有一个int id,字符串名称,byte []图片和Orders的IList。
Order class has an int id, string name, byte[] picture and IList of Products. Order类有一个int id,string name,byte []图片和IList of Products。
Product class has an int id, string name, byte[] picture, string productstr and int quantity (which I dont currently use) Product类有一个int id,string name,byte [] picture,string productstr和int quantity(我目前不使用)
Customer table has a customer id, name and picture(varbinary(max)). 客户表具有客户ID,名称和图片(varbinary(max))。
Order has order id, name, picture and a customer id 订单包含订单ID,名称,图片和客户ID
Product has a product id, name, picture, productstr and order id. 产品有产品ID,名称,图片,产品和订单ID。
The problem : When I delete a customer using session.Delete(csCustomer), it succesfuly deletes the entire customer from the db but it doesnt delete all of its orders. 问题:当我使用session.Delete(csCustomer)删除客户时,它会成功地从数据库中删除整个客户,但它不会删除所有订单。 It only puts null in the customer id field in all of the orders of the deleted customer.
它仅在已删除客户的所有订单中的客户ID字段中放置null。
Anyone can find a problem with my configuration? 任何人都可以找到我的配置问题? I saw an example of using nhibernate where they saved a reference to the customer in the order class and a reference to order in the product class, is that something I need to do to fix it?
我看到了一个使用nhibernate的例子,他们在订单类中保存了对客户的引用,并在产品类中引用了订单,我需要做些什么来修复它?
You have to set inverse="true"
on your bag. 你必须在你的包上设置
inverse="true"
。
Your Customer mapping should look like this: 您的客户映射应如下所示:
<class name="Customer" table="Customers" lazy="false">
<id name="Id" column="CustomerId">
<generator class="native">
</id>
<property name="Name" />
<property name="Picture" type="BinaryBlob" />
<bag name="Orders" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="CustomerId" />
<one-to-many class="Order" />
</bag>
</class>
There's a good article about this property here and Ayende has written about it long time ago. 有一个关于该物业的好文章在这里和Ayende已经写很久以前的事了。
I would suggest you to extend your Order mapping referencing your customer adding: 我建议您扩展引用您的客户的订单映射添加:
<many-to-one name="Customer" column="CustomerId" not-null="true"/>
and add the virtual property to your class: 并将虚拟属性添加到您的类:
public virtual Customer Customer { get; set; }
Some more information here . 这里有更多信息。
Another thing you might want to consider is change the bag
with a set
cause as bag allows duplicates, as explained here . 你可能要考虑的另一件事是改变
bag
用set
事业为包允许重复,如解释在这里 。
And maybe strengthen your domain using encapsulation . 并且可能使用封装来增强您的域名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.