How to create a tagging system with NHibernate (many to many)
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:
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.
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 c开发者_如何学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.
Have a look at this answer I gave a while ago:
What is the correct way to define many-to-many relationships in NHibernate to allow deletes but avoiding duplicate records
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.
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?
精彩评论