【问题标题】:In Grails, need to define a Set for one-to-many relationship?在 Grails 中,需要为一对多关系定义一个 Set 吗?
【发布时间】:2010-09-28 15:23:30
【问题描述】:

我有 Grails 域类 Child 和 Toy。一个孩子可以有很多玩具。

class Child {
    static hasMany = [ toys : Toy ]
    String name
}

class Toy {
    static belongsTo = [ owner : Child ]
    String name
}

据我了解,Child 上会有一个玩具财产。也就是会有 Child 方法:

public Set getToys()

但这不起作用。我无法从 Java 类中引用 child.getToys()。我在 Child 中明确定义了玩具:

class Child {
    static hasMany = [ toys : Toy ]
    String name
    Set toys
}

这是正确的吗?我是否需要为一对多关系显式定义一个 Set?

【问题讨论】:

    标签: grails


    【解决方案1】:

    是的,如果您想从 Java 中引用它,您必须明确定义该属性。这与 groovy 编译器 (groovyc) 为 groovy bean 创建存根类的方式有关。

    【讨论】:

    • 查看我在 user@grails.codehaus.org 上的帖子。声称这已在 Grails 1.1 中修复。 nabble.com/…
    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多