Note that there are some explanatory texts on larger screens.

plurals
  1. POBidirectional one to many (or many to one) cascade delete behaviour. It works, but why?
    primarykey
    data
    text
    <p>I have two Nhibernate mappings for two classes, Category and Product. My Category class has two properties that are collections. The Children property is a collection of type Category which represents child categories (represents a category menu, typical parent child scenario). The second property on the Category class is a Products collection which represents all the products under a category.</p> <p>What I am trying achieve is when I delete a category I want the category to deleted but not the product. So I want the product to be orphaned. i.e have its foreign key (CategoryId) in the Product table set to null. I don't want to delete a product just because I have deleted a category. I want to be able to reassign in at a later time to another category. My mappings representing the mentioned scenario are below.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="naakud.domain" namespace="naakud.domain"&gt; &lt;class name="Category"&gt; &lt;id name="Id"&gt; &lt;generator class="hilo" /&gt; &lt;/id&gt; &lt;version name="Version"/&gt; &lt;property name="Name" not-null="true" unique="true" /&gt; &lt;set name="Products" cascade="save-update" inverse="true" access="field.camelcase-underscore"&gt; &lt;key column="CategoryId" foreign-key="fk_Category_Product" /&gt; &lt;one-to-many class="Product" /&gt; &lt;/set&gt; &lt;many-to-one name="Parent" class="Category" column="ParentId" /&gt; &lt;set name="Children" collection-type="naakud.domain.Mappings.Collections.TreeCategoriesCollectionType, naakud.domain" cascade="all-delete-orphan" inverse="true" access="field.camelcase-underscore"&gt; &lt;key column="ParentId" foreign-key="fk_Category_ParentCategory" /&gt; &lt;one-to-many class="Category"/&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="naakud.domain" namespace="naakud.domain"&gt; &lt;class name="Product"&gt; &lt;id name="Id"&gt; &lt;generator class="hilo" /&gt; &lt;/id&gt; &lt;version name="Version" /&gt; &lt;property name="Name" not-null="true" unique="true" /&gt; &lt;property name="Description" not-null="true" /&gt; &lt;property name="UnitPrice" not-null="true" type="Currency" /&gt; &lt;many-to-one name="Category" column="CategoryId" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>With this mapping, when I delete a category which has products associated with it I get the following constraint error.</p> <p>The DELETE statement conflicted with the REFERENCE constraint "fk_Category_Product". The conflict occurred in database "naakud", table "dbo.Product", column 'CategoryId'. The statement has been terminated.</p> <p>However, when I remove the inverse=true attribute on the Products collection in the Category mapping then it works fine. My CategoryId foreign key in the products table is set to null and thus disassociating a product with a category. Which is what I want.</p> <p>I have read about the inverse attribute and I understand it signifies the owning side of a relationship and updates/inserts/deletes are done in a different order which is why I think it solves my problem. So my question is, am I solving my problem in the correct way? How does this affect performance? (not much I suspect). Would it be better to have a uni-directional relationship without the many to one side and have the inverse attribute set to true to get better performance? Or am I going crazy and completely missing the point? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload