C# – How to create a tagging system with NHibernate (many to many)

cnhibernatenhibernate-mappingorm

I'm learning NHibernate and hoping you guys could help me a bit about Tag Cloud design and solutions.

I have 3 tables which are "News", "Tags" and "News_Tags" with Many-To-Many relationship and the "News_Tags" is the link table.

Options:

  1. cascade="all", cascade="all-delete-orphan" If I delete one of the news records, the it will delete all my news records which have the same tags.

  2. cascade="save-update" It works with save and update, but if I try to delete news it will give the error: deleted object would be re-saved by cascade (remove deleted object from associations)

Here is my mappings:

Tags:

<class name="Tag" table="Tags" lazy="false">
    <id name="TagID">
    <generator class="identity" />
    </id>
    <property name="TagName" type="String"></property>
    <property name="DateCreated" type="DateTime"></property>

    <!--inverse="true" has been defined in the "News mapping"-->
    <set name="NewsList" table="New_Tags" lazy="false" cascade="all">
    <key column="TagID" />
    <many-to-many class="New" column="NewID" />
    </set>
</class>

News:

<class name="New" table="News" lazy="false">
<id name="NewID">
  <generator class="identity" />
</id>
<property name="Title" type="String"></property>
<property name="Description" type="String"></property>

<set name="TagsList" table="New_Tags" lazy="false" inverse="true" cascade="all">
  <key column="NewID" />
  <many-to-many class="Tag" column="TagID" />
</set>
</class>

Can anyone provide some solutions for this?
@Lck mentioned I could do this manually, can anyone provide some code sample for me? Thank you very much.

Best Answer

With cascade="all", deleting a News object should simply delete all the corresponding rows in the New_Tags table, shouldn't it? I don't think it would delete all the News items that are tagged that way. Is this not the behaviour you want?