Grails GORM 自引用属于删除与预期相反的方向

发布于 2024-10-28 08:39:59 字数 559 浏览 7 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

可是我不能没有你 2024-11-04 08:39:59

如果我理解正确,一个类别属于一个父级,并且一个父级可以有多个子级,所以我认为您需要一个 hasMany 关系,如下所示:

class Category {
    String name

    static mapping = {
        cache true
        name index:'category_name_idx'
    }

    static belongsTo = [parent:Category]
    static hasMany = [children: Category]

    static constraints = {
        parent(nullable:true)
    }
}

我有类似的结构,并且从来没有以这种方式进行删除的问题。

希望这有帮助!

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:

class Category {
    String name

    static mapping = {
        cache true
        name index:'category_name_idx'
    }

    static belongsTo = [parent:Category]
    static hasMany = [children: Category]

    static constraints = {
        parent(nullable:true)
    }
}

I had had similar structures and never have issues with the delete doing it this way.

Hope this helps!

挽心 2024-11-04 08:39:59

这不是答案,但我找到了解决我自己问题的方法。您可以删除belongsTo = [parent:Category],用一个简单的实例变量替换它。这会阻止 subCategory.delete() 级联到父级。

class Category {
    String name
    Category parent

    static mapping = {
        cache true
        name index:'category_name_idx'
    }

    static constraints = {
        parent(nullable:true)
    }
}

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.

class Category {
    String name
    Category parent

    static mapping = {
        cache true
        name index:'category_name_idx'
    }

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