Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need to do in order to have your mappings work as you would expect them to, is to move the <code>inverse="true"</code> from the <code>Category.Items</code> collection to the <code>Item.Categories</code> collection. By doing that you will make NHibernate understand which one is the owning side of the association and that would be the "Category" side. </p> <p>If you do that, by deleting a Category object it would delete the matching record from the lookup table as you want it to as it is allowed to do so because it is the owning side of the association.</p> <p>In order to NOT delete the Items that are assigned to a Category object that is to be deleted you need to leave have the cascade attribe as: <code>cascade="save-update"</code>.</p> <p><code>cascade="all"</code> will delete the items that are associated with the deleted Category object.</p> <p>A side effect though would be that deleting the entity on the side where the inverse=tru exists will thow a foreign key violation exception as the entry in the association table is not cleared.</p> <p>A solution that will have your mappings work exactly as you want them to work (by the description you provided in your question) would be to explicitly map the association table. Your mappings should look like that:</p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="..." namespace="..."&gt; &lt;class name="Category" lazy="true"&gt; &lt;id name="CategoryId" unsaved-value="0"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="Name" /&gt; &lt;bag name="ItemCategories" generic="true" inverse="true" lazy="true" cascade="none"&gt; &lt;key column="CategoryId"/&gt; &lt;one-to-many class="ItemCategory"/&gt; &lt;/bag&gt; &lt;bag name="Items" table="ItemCategory" cascade="save-update" generic="true"&gt; &lt;key column="CategoryId"&gt;&lt;/key&gt; &lt;many-to-many class="Item" column="ItemId"&gt;&lt;/many-to-many&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="..." namespace="..."&gt; &lt;class name="Item" table="Item" lazy="true"&gt; &lt;id name="ItemId" unsaved-value="0"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="Name" /&gt; &lt;bag name="ItemCategories" generic="true" inverse="true" lazy="true" cascade="all-delete-orphan"&gt; &lt;key column="ItemId"/&gt; &lt;one-to-many class="ItemCategory"/&gt; &lt;/bag&gt; &lt;bag name="Categories" table="ItemCategory" inverse="true" cascade="save-update" generic="true"&gt; &lt;key column="ItemId"&gt;&lt;/key&gt; &lt;many-to-many class="Category" column="CategoryId"&gt;&lt;/many-to-many&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>As it is above it allows you the following:</p> <ol> <li>Delete a Category and only delete the entry in the association table without deleting any of the Items</li> <li>Delete an Item and only delete the entry in the association table without deleting any of the Categories</li> <li>Save with Cascades from only the Category side by populating the Category.Items collection and saving the Category.</li> <li>Since the inverse=true is necessary in the Item.Categories there isn't a way to do cascading save from this side. By populating the Item.Categories collection and then saving the Item objec you will get an insert to the Item table and an insert to the Category table but no insert to the association table. I guess this is how NHibernate works and I haven't yet found a way around it.</li> </ol> <p>All the above are tested with unit tests. You will need to create the ItemCategory class mapping file and class for the above to work.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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