简体   繁体   English

NHibernate一对多删除不级联

[英]NHibernate One-To-Many Delete Not Cascading

I have a 'Photo' class and a 'Comment' class. 我有一个'Photo'课程和一个'Comment'课程。 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. 我在HBM映射文件中将其配置为一对多关系,并对Photo.hbm.xml映射文件中的“注释”包设置了cascade =“all-delete-orphan”。

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"' 但是,如果我尝试删除与其关联的一条或多条评论的照片,我会收到'DELETE语句与REFERENCE约束冲突“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. 我在Photo.hbm.xml中针对Comments包尝试了几个其他级联选项,但无论我将其设置为什么,我每次都得到相同的结果。 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. 更新:我可以让级联发生的唯一方法是在SQL Server中配置“删除规则”,将此关系配置为“级联”,这样做意味着我不需要在我的NHibernate中指定任何级联操作映射。 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? 但是,这对我来说并不理想 - 我希望能够在NHibernate Mapping中理想地配置级联行为,所以我仍然感到困惑,为什么它似乎没有注意到我的NHibernate级联设置?

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". 我的猜测是问题来自于Comment映射中的多对一设置为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. 因此,NHibernate在删除Photo对象之前不允许暂时将此属性设置为null,因此在删除Photo对象时,SQL Server会抛出外键异常。

If I remember correctly for the order of actions when deleting is: 如果我在删除时正确记住了操作顺序:

  1. Set foreign key value to null in all child objects 在所有子对象中将外键值设置为null
  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. 尝试从多对一中删除not-null =“true”,看看会发生什么。

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

I had similar problem for 1 day .. and got frustrated over it. 我有一天类似的问题..并对此感到沮丧。

Finally the solution boiled down to the DB. 最后,解决方案归结为DB。 I had to change the FK key constraints in "INSERT UPDATE SPECIFICATION" 'Delete Rule' : from 'No Action' to 'Cascade' 我必须在“INSERT UPDATE SPECIFICATION”'删除规则'中更改FK键约束:从'No Action'到'Cascade'

additionally you can also set 'Update Rule' : from 'No Action' to 'Cascade' 此外,您还可以设置“更新规则”:从“无操作”到“级联”

You can specify the delete-cascade option in NH: 可以在NH中指定delete-cascade选项:

<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. 然后我想知道你的FK_Comments_Photos列在哪里指定。

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

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