R – NHibernate Many to Many delete all the data in the table

nhibernate

I would love to thank @Stefan Steinegger and @David helped me out yesterday with many-to-many mapping.

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

If I delete one of the news records, the following mappings will delete all my news records which have the same tags. One thing I need to notice, I only allowed unique tag stored in the "Tag" table.

This mapping make sense for me, it will delete the tag and related News records, but how can I implement a tagging system with NHibernate?

Can anyone give me some suggestion? Many thanks.

Daoming.

News Mapping:

<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>

Tag Mapping:

<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>

Best Answer

When I run into trouble like that, the first thing I twiddle with is the cascade option.