简体   繁体   English

如何使用NHibernate创建标签系统(很多)

[英]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. 我正在学习NHibernate,希望你们能对标签云设计和解决方案有所帮助。

I have 3 tables which are "News", "Tags" and "News_Tags" with Many-To-Many relationship and the "News_Tags" is the link table. 我有3个表,它们是具有多对多关系的“新闻”,“标签”和“ News_Tags”,“ News_Tags”是链接表。

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. Cascade =“ all”,cascade =“ all-delete-orphan”如果删除新闻记录之一,它将删除所有具有相同标签的新闻记录。

  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) Cascade =“ save-update”它与保存和更新一起使用,但是如果我尝试删除新闻,则会出现错误:级联重新保存已删除的对象(从关联中删除已删除的对象)

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? @Lck提到我可以手动执行此操作,有人可以为我提供一些代码示例吗? Thank you very much. 非常感谢你。

With cascade="all" , deleting a News object should simply delete all the corresponding rows in the New_Tags table, shouldn't it? 使用cascade="all" ,删除News对象应该只删除New_Tags表中的所有相应行,不是吗? I don't think it would delete all the News items that are tagged that way. 我认为它不会删除所有以这种方式标记的新闻项目。 Is this not the behaviour you want? 这不是您想要的行为吗?

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 在NHibernate中定义多对多关系以允许删除但避免重复记录的正确方法是什么

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. 它不是直接回答您的问题,而是通过其极端的解决方案和得到的评论,您可能会了解要真正实现所需的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM