使用 "soft" 加入的 Grails GORM 域 类 问题

Issue on Grails GORM domain classes with "soft" joins

我已经在 Domain 类 上映射了两个 table,比如:

我想指出的是,没有在数据库中明确声明的特定 foreign keys,而是在 GORM 的映射中声明的。但这里有一个问题:有一些特殊类型的 Child 是孤儿——也就是说,没有它们的 Parent table 对应物。每次我通过有效的 Child.findById() 访问这些孤儿时,都会显示错误:

Message: No row with the given identifier exists

访问非孤儿时不存在 Child。我已经整理了这些 Domain table,每次 Child 被解析为 JSON 时,它都会有一个 属性 称为 parentsParent 的数组,反之亦然。它已经在工作了——除了这些孤儿的情况。

我该如何解决这个问题?我是否应该删除他们的 GORM 连接,因为它们实际上在数据库端并不是联合的?

我假设您的域 类 如下所示:

class Parent {
    static hasMany = [children: Child]
}

class Child {
    static belongsTo = [parent: Parent]
}

那是 bi-directional one-to-many association。 GORM 期望 child table 包含指向 parent 的外键,而 child 只有在具有 parent.[=12= 时才能存在]

允许孤儿 children 的一种方法是按照您的建议进行操作:删除 gorm 关联。另一种方法是删除 belongsTo 以创建 uni-directional 关联。这样 child 可以在没有 parent 的情况下存在。