views:

62

answers:

2

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.

A: 

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?

David M
A: 

Have a look at this answer I gave a while ago:

http://stackoverflow.com/questions/1695063/what-is-the-correct-way-to-define-many-to-many-relationships-in-nhibernate-to-all/1697978#1697978

It is not answering your quesiton directly but through its extreme solution and the comments it got you may understand what needs to be done to achieve exactly what you need.

tolism7
I have spent 3 nights on this and almost give up. I put the inverse="true" into a wrong mapping file. Thank very much. Did you write any blogs or articles so I can follow them. :)
Daoming Yang
You are welcome. I do not have a blog at the moment but I am working on it ;-)
tolism7