如何在两者之间设置适当的关系 类

How to set proper relation among two classes

我在设置两个 class 之间的关系时遇到一些问题。我有 2 classes,学生:

class Student {
   String name
   Guardian father
   Guardian mother
   Guardian local_guardian
}

和监护人:

class Guardian {
   String name;
   static hasMany = [children:Student]
   static mappedBy = [children:'father']
}

在这里,我使用 mappedBy 将 Guardian object 映射到父亲 属性 。除非 mappedBy,否则我会收到错误提示,应该将 mappedBy 与 3 个 Student class 属性 中的任何一个一起使用。 我尝试通过此查询输入一些示例数据

new Student(name:"John", father: new Guardian(name:"Michael").save(),
            mother: new Guardian(name:"Mary").save(),
            local_guardian: new Guardian(name:"James").save()).save(flush:true);

数据已成功保存,但我的问题是,由于我将 mappedBy 与 'father' 属性 一起使用,所以我只能对那个父亲 object 使用 Guardian.children ]. 当我尝试获取 children with mother 和 local_guardian object 的列表时, (例如:mother.children)得到空结果。 我尝试在许多方面添加 addTo,例如

Guardian.findByName("Mary").addToChildren(
   Student.findByName("John")).save(flush:true);

并尝试访问

Guardian.findByName("Mary").children 

在这里,我得到了结果,但是它将 child 从父亲移到了母亲 object,并且不再能够访问 father.children 我将如何解决这种情况? 我想要实现的是,我应该能够从 Guardian object 的所有 3 个列表中获得 children 的列表。这里一个学生 object 指着 3 个监护人 objects (father, mother, local_guardian)。所以我应该可以通过

得到children的名单
  1. father.children
  2. mother.children
  3. local_guard.children

我如何设置这些 class 之间的正确关系来解决我的问题?

您可能想使用 hasManybelongsTo,然后要定义监护人,您可能想在监护人 object 中使用诸如父亲、母亲、localGuardian 属性之类的东西。有了这样的关系,您就可以使用 transients 来定义 children 和 mother / father 的集合。

例如

 class Student
     {
        String name
        Guardian local_guardian
        static belongsTo = [primaryGuardian: Guardian]
        static transients=['mother', 'father']
        //define the transients
        def getMother(){
            if(primaryGuardian.type == 'mother') {
               return primaryGuardian
           } else {
             return primaryGuardian.spouse
          }
        }
        //do something similiar for getFather
      }

     Class Guardian
        {
         String name
         String type
         Guardian spouse
         static hasMany = [children:Student, localGuardianStudents: Student]
        }    

请注意,这只是示例代码,可能包含错误,因为我没有对其进行测试。
所以你可以接着创建一个监护人,然后通过调用

添加children
guardian.addToChildren(child)

无论如何,这会让你通过调用 guardian.children 获得监护人的 children,它让你获得 [=29= 的主要监护人(母亲或父亲) ] 通过调用 child.primaryGuardian,它可以让你通过调用 child.mother 获取母亲或父亲,而无需在其中添加特殊类型。

您需要确保修改此处的约束,以便配偶可以为空。此外,这种性质的关系在创建它们时有时会变得棘手,导致在您尝试保存时出现与丢失 ID 号相关的错误,因此您要确保在创建和修改这些关系时定义了关系的双方object秒。

如果你想使用 hasMany 实现这个关系,那么你需要在 Guardian 中有三个 mappedBy class。

static hasMany = [children:Student, motherChildres:Student, localGuardianChildrens:Student]
   static mappedBy = [children:'father', motherChildrens:'mother', localGuardianChildrens: 'local_guardian']

但这看起来不太好,相反,您可以使用中间层域 class 实现关系,并在 Guardian class 中添加 addToChildren 和 getChildren 方法,如下所示。

class GuardianChildren {
   Guardian guardian
   Student student

   constraints {
     student unique: ['guardian']
   }

}

Guardian {
  void addToChildrens(Student child) {
      new GuardianChildren(guardian:this, student:child).save()
   }

   @Transient
   List<Student> getChildrens() {
      return  GuardianChildren.findAllByGuardian(this).children
  }
}

Student {

   @Transient
   Guardian getMother() {
     //find guardin children where guardian type is mother and children is this student
   }

  @Transient
  Guardian getFather() {..}
}

从 Guardian 中删除 hasMany,从 Student 中删除 father/mother 属性。您可能还需要 Guardian 中的类型字段来指定这是否是 mother/father 等