NHibernate Many to Many delete all my data in the table

Posted by Daoming Yang on Stack Overflow See other posts from Stack Overflow or by Daoming Yang
Published on 2010-01-04T23:37:29Z Indexed on 2010/04/02 18:03 UTC
Read the original article Hit count: 282

Filed under:

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>

© Stack Overflow or respective owner

Related posts about nhibernate