Grails GORM 自引用属于删除与预期相反的方向
我有一个 Grails 域类,它是类别的层次结构。每个类别都有一个父类别(根类别除外,该类别为空)。
class Category {
String name
static mapping = {
cache true
name index:'category_name_idx'
}
static belongsTo = [parent:Category]
static constraints = {
parent(nullable:true)
}
}
我的问题:删除级联与我的预期完全相反:
- someSubCategory.delete() 删除类别,然后尝试删除父类别(如果父类别有其他子类别,则会因完整性违规而失败)。
- ParentCategory.delete() 不会级联删除其子级,而是会因完整性冲突而失败。
我做错了什么?我的理解是,上面的“belongsTo”应该告诉 GORM 将删除从父级级联到所有子级,而不是从子级级联到其父级。
I have a Grails domain class that is a hierarchy of categories. Each Category has a parent category (except for the root category which is null).
class Category {
String name
static mapping = {
cache true
name index:'category_name_idx'
}
static belongsTo = [parent:Category]
static constraints = {
parent(nullable:true)
}
}
My problem: deletes cascade exactly opposite of what I'd expect:
- someSubCategory.delete() deletes the category then tries to delete the parent category (which fails with an integrity violation if the parent has other children).
- parentCategory.delete() does NOT cascade delete its children, but instead just fails with an integrity violation.
What am I doing wrong? My understanding is that the 'belongsTo' above should tell the GORM to cascade deletes from the parent to all children, but not from a child to its parent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,一个类别属于一个父级,并且一个父级可以有多个子级,所以我认为您需要一个 hasMany 关系,如下所示:
我有类似的结构,并且从来没有以这种方式进行删除的问题。
希望这有帮助!
If I am understanding correctly a Category belongs to a parent and a parent can have multiple children, so I think you need a hasMany relationship, something like this:
I had had similar structures and never have issues with the delete doing it this way.
Hope this helps!
这不是答案,但我找到了解决我自己问题的方法。您可以删除belongsTo = [parent:Category],用一个简单的实例变量替换它。这会阻止 subCategory.delete() 级联到父级。
It's not an answer, but I found a workaround to my own question. You can remove the belongsTo = [parent:Category], replacing it with a simple instance variable. This stops subCategory.delete() from cascading to the parent.