NHibernate多对多删除我表中的所有数据 [英] NHibernate Many to Many delete all my data in the table

查看:130
本文介绍了NHibernate多对多删除我表中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要感谢@Stefan Steinegger和@David昨天通过多对多映射帮助了我.

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

我有3个表,它们是具有多对多"关系的新闻",标签"和"News_Tags",而"News_Tags"是链接表.

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.

此映射对我来说很有意义,它将删除标签和相关的新闻记录,但是如何使用NHibernate实施标签系统?

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.

道明.

新闻映射:

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>

推荐答案

使用级联选项保存更新".

Use the cascade option "save-update".

选项全部"将级联删除,这种情况下您不希望这样做.但是,您选择无"将要求Tag实体已经存在,我想可能并非总是如此.

The option "all" will cascade deletes, which you do not want in this case. But you the option "none" will require that the Tag entity is already persisted which I guess might not always be the case.

因此,通过将级联设置为保存更新",直到将新标签插入标签"表和链接表News_Tags中,但是当您从新闻"实体中删除标签时,它只会从链接表中删除,而不会标签表.

So by setting the cascade to "save-update" new Tags till be inserted in the Tags table and in the link table News_Tags, but when you remove a tag from a News entity it will only be removed from the link table not the Tags table.

这篇关于NHibernate多对多删除我表中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆