Grails - 多个属于同级的级联删除 [英] Grails - multiple belongsTo of same class with cascading deletion

查看:170
本文介绍了Grails - 多个属于同级的级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个是针对Grails用户的。我在grails - 用户邮件列表上问过它,但是我想,因为我已经打了几天,所以我应该尽可能使用广泛的网络。

我试图在引用
两个对象的另一个对象(不同类型)中为相同类型的两个
对象建立关系时遇到了一些困难。



作为我想要做的一个例子,假设您正在建模家庭成员之间的关系
。任何给定的关系
属于两个不同的家庭成员。所以:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' [关系:'p1',关系:'p2']
}

类关系{

人员p1
人员p2
字符串natureOfRelationship //例如cousins

static belongsTo = [p1:Person,p2:Person]
}

这里的意图是,如果p1或p2被删除,那么删除将
级联到hasMany映射中的所有关系对象。相反,每一次
我都会尝试,最终导致外键违规。我尝试使用文档中涵盖的
cascade属性:

http://grails.org/doc/1.0.x/guide/single.html#5.5.2.9%20Custom% 20Cascade%20Behaviour



所以我想我会把它添加到Person类中:

 静态映射= {
关系级联:'删除'
}

我没有任何运气。



我还查看了Grails生成的devDB.script文件,以了解它如何b $ b在Relationship上设置外键。如果我手动将ON
DELETE CASCADE添加到外键约束中,那么它工作正常,但
明显对自动生成的数据库脚本
进行手动编辑并不是最强大的解决方案。理想情况下,我希望能够指定使用GORM行为的



所以我最好的选择是什么?有没有办法强制对
多个外键/所有者进行级联删除?我是否需要在Person上使用
onDelete动作手动执行此操作?我需要进入Hibernate配置
这个,还是我可以在Grails / GORM中以某种方式执行?



非常感谢您的时间和您可以提供任何帮助。

解决方案

您可以添加 beforeDelete 挂钩到Person类并查询另一个父级。如果其他父母不存在,则可以删除关系。请注意,您正在碰到外键违规行为,因为您可能需要删除双方的父母,因为关系对他们都有FK。


This one is for the Grails users here. I asked it on the grails - user mailing list, but I figured since I've been fighting this for a couple of days I should cast as wide a net as possible.

I'm having some difficulty with trying to model relationships between two objects of the same type in another object (different type) referencing the two objects.

As an example of what I'm trying to do, suppose you're modeling relationships among family members. Any given relationship "belongsTo" two different family members. So:

class Person {
   hasMany[relationships: Relationship]

   static mappedBy = [relationships:'p1', relationships:'p2']
}

class Relationship {

   Person p1
   Person p2
   String natureOfRelationship // for example, "cousins"

   static belongsTo = [p1: Person, p2: Person]
}

The intent here is that if either p1 or p2 is deleted, then the delete will cascade to all Relationship objects in the hasMany map. Instead, every time I try it, I end up with a foreign key violation. I tried using the "cascade" attribute as covered in the documentation:

http://grails.org/doc/1.0.x/guide/single.html#5.5.2.9%20Custom%20Cascade%20Behaviour

So I figured I would add this to the Person class:

static mapping = {
    relationships cascade:'delete'
}

I didn't have any luck with that either.

I also looked at the devDB.script file that Grails generates, to see how it was setting up the foreign keys on Relationship. If I manually add "ON DELETE CASCADE" to both foreign key constraints, then it works fine, but obviously doing manual edits to an automatically generated database script is not the most robust solution. Ideally I'd like to be able to specify that behavior using GORM.

So what's my best bet here? Is there a way to force cascading deletes on multiple foreign keys/owners? Would I need to do this manually with an onDelete action on Person? Do I need to get into Hibernate configs for this, or can I do it in Grails/GORM some way?

Thanks very much for your time and for any assistance you can offer.

解决方案

You can add a beforeDelete hook to the Person class and query the other parent. If the other parent does not exist, you can delete the relationship. Note that you are hitting foreign key violations because you probably need to delete both parents, since the relationship has an FK to both of them.

这篇关于Grails - 多个属于同级的级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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