NHibernate一对多删除不级联

发布于 2024-10-13 07:47:00 字数 1969 浏览 9 评论 0原文

我有一个“照片”课程和一个“评论”课程。一张照片可以分配多个评论。

我已在 HBM 映射文件中将其配置为一对多关系,并针对 Photo.hbm.xml 映射文件中的“注释”包设置了cascade="all-delete-orphan"。

但是,如果我尝试删除具有 1 个或多个与之关联的评论的照片,我会收到“删除语句与 REFERENCE 约束“FK_Comments_Photos”冲突”

我针对照片中的评论包尝试了其他几个级联选项.hbm.xml 但无论我将其设置为什么,我每次都会得到相同的结果。我只是希望能够删除照片并自动删除任何相关的评论。

这是我的照片映射(为简洁起见进行了编辑):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" .... default-access="property" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Photo" table="Photos">
    <id name="PhotoId" unsaved-value="0">
        <column  name="PhotoId" />
        <generator class="native" />
    </id>
    ...
    <bag name="Comments" table="Comments" cascade="all-delete-orphan" order-by="DateTimePosted desc" where="Approved=1">
        <key column="PhotoId" />
        <one-to-many class="Comment" />
    </bag>
</class>

这是我的评论映射(为简洁起见进行了编辑):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ... default-access="property" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Comment" table="Comments">
    <id name="CommentId" unsaved-value="0">
        <column name="CommentId"></column>
        <generator class="native" />
    </id>
    ...
    <property name="Author" not-null="true" />
    <property name="Body" not-null="true" />
    <property name="Approved" not-null="true" />
    <many-to-one name="Photo" not-null="true">
        <column name="PhotoId" />
    </many-to-one>
</class>

有人对为什么当我尝试删除带有相关评论的照片时没有发生级联有任何建议吗?

更新:我可以实现级联的唯一方法是根据与“级联”的这种关系在 SQL Server 中配置“删除规则”,这样做意味着我不需要指定我的 NHibernate 映射中的任何级联操作。然而,这对我来说并不理想 - 我希望能够理想地配置 NHibernate 映射中的级联行为,所以我仍然很困惑为什么它似乎没有注意到我的 NHibernate级联设置?

I have a 'Photo' class and a 'Comment' class. An Photo can have multiple comments assigned to it.

I have this configured as a one-to-many relationship within my HBM mapping file, and have set cascade="all-delete-orphan" against the 'Comments' bag within the Photo.hbm.xml mapping file.

However, if I try to delete a Photo which has 1 or more Comments associated with it, I am getting 'The DELETE statement conflicted with the REFERENCE constraint "FK_Comments_Photos"'

I tried a couple of other cascade options against the Comments bag in my Photo.hbm.xml but regardless of what I set it to, I'm getting the same outcome each time. I just want to be able to delete a Photo and have any associated comments automatically delete too.

Here is my Photo mapping (edited for brevity):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" .... default-access="property" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Photo" table="Photos">
    <id name="PhotoId" unsaved-value="0">
        <column  name="PhotoId" />
        <generator class="native" />
    </id>
    ...
    <bag name="Comments" table="Comments" cascade="all-delete-orphan" order-by="DateTimePosted desc" where="Approved=1">
        <key column="PhotoId" />
        <one-to-many class="Comment" />
    </bag>
</class>

Here is my Comment mapping (edited for brevity):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ... default-access="property" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Comment" table="Comments">
    <id name="CommentId" unsaved-value="0">
        <column name="CommentId"></column>
        <generator class="native" />
    </id>
    ...
    <property name="Author" not-null="true" />
    <property name="Body" not-null="true" />
    <property name="Approved" not-null="true" />
    <many-to-one name="Photo" not-null="true">
        <column name="PhotoId" />
    </many-to-one>
</class>

Does anyone have any suggestions as to why the cascade is not happening when I try to delete a Photo with comments associated with it?

UPDATE: The only way I can get the cascade to happen is to configure the 'Delete Rule' within SQL Server against this relationship to 'Cascade', and in doing so means that I don't need to specify any cascade action within my NHibernate Mapping. However, this isn't ideal for me - I'd like to be able to configure the cascade behaviour within the NHibernate Mapping ideally, so I'm still confused as to why it doesn't appear to be taking any notice of my NHibernate cascade setting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦过后 2024-10-20 07:47:00

我的猜测是,问题来自于注释映射中的多对一设置为 not-null="true" 的事实。
因此,NHibernate 在删除 Photo 对象之前不允许暂时将此属性设置为 null,因此在删除 Photo 对象时 SQL Server 会抛出外键异常。

如果我没记错的话,删除时的操作顺序是:

  1. 在所有子对象中将外键值设置为 null
  2. 删除父对象
  3. 删除所有子引用

尝试从多对一中删除 not-null="true" 并查看会发生什么。

My guess would be that the problem comes from the fact that the many-to-one in the Comment mapping is set to not-null="true".
Because of that, NHibernate is not allowed to set this property to null temporarily before it deletes the Photo object and therefore when is goes about deleting the Photo object SQL Server throws an foreign key exception.

If I remember correctly for the order of actions when deleting is:

  1. Set foreign key value to null in all child objects
  2. Delete parent object
  3. Delete all child references

Try to remove the not-null="true" from the many-to-one and see what will happen.

ゝ杯具 2024-10-20 07:47:00

尝试在映射的包集合上使用 inverse="true"

Try with inverse="true" on the bag collection of your mapping.

乄_柒ぐ汐 2024-10-20 07:47:00

我有 1 天遇到类似的问题..并且对此感到沮丧。

最后解决方案归结为数据库。
我必须更改“INSERT UPDATE SPECIFICATION”中的 FK 键约束
“删除规则”:从“无操作”到“级联”

此外,您还可以设置
“更新规则”:从“无操作”到“级联”

I had similar problem for 1 day .. and got frustrated over it.

Finally the solution boiled down to the DB.
I had to change the FK key constraints in "INSERT UPDATE SPECIFICATION"
'Delete Rule' : from 'No Action' to 'Cascade'

additionally you can also set
'Update Rule' : from 'No Action' to 'Cascade'

时光匆匆的小流年 2024-10-20 07:47:00

可以在 NH 中指定删除级联选项:

<bag name="Comments" cascade="all-delete-orphan" order-by="DateTimePosted desc" where="Approved=1">
    <key column="PhotoId" on-delete="cascade"/>
    <one-to-many class="Comment" />
</bag>

您可能应该将其反转。然后我想知道你的 FK_Comments_Photos 列是在哪里指定的。

You can specify the delete-cascade option in NH:

<bag name="Comments" cascade="all-delete-orphan" order-by="DateTimePosted desc" where="Approved=1">
    <key column="PhotoId" on-delete="cascade"/>
    <one-to-many class="Comment" />
</bag>

You probably should make it inverse. Then I wonder where your FK_Comments_Photos column is specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文