【问题标题】:Override belongsTo / hasMany in a Grails subclass覆盖 Grails 子类中的 belongsTo / hasMany
【发布时间】:2014-02-17 18:00:55
【问题描述】:

我有这 4 个课程:

class Animal {
    static hasMany = [legs: Leg]
}

class Cat extends Animal {
    static hasMany = [legs: CatLeg] // Should I remove this line?
}

class Leg {
    static belongsTo = [animal: Animal]
}

class CatLeg extends Leg {
    static belongsTo = [animal: Cat] // Should I remove this line?
}

这样可以吗?是否有必要或建议在子类中再次声明这些变量?添加或省略它们有什么区别?

我目前正在使用 Grails 2.2.2。

谢谢,

乔迪

【问题讨论】:

    标签: grails grails-domain-class


    【解决方案1】:

    这取决于您的休眠设置。默认情况下,在 Grails 中,Hibernate 将对象的层次结构映射到 1 个表中(每个表的层次结构)。这意味着,您不需要在子类中再次声明引用。 GORM 层会处理正确的类(Leg 与 CatLeg)。

    一般来说,我建议完全避免使用对象层次结构并将其替换为组合,特别是如果您有一个引用对象的层次结构。我认为,如果将来需求发生变化,您可能会遇到麻烦,将数据迁移到新模型。

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 2014-03-15
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2015-09-13
      相关资源
      最近更新 更多