在Grails中,需要为一对多关系定义Set吗? [英] In Grails, need to define a Set for one-to-many relationship?

查看:84
本文介绍了在Grails中,需要为一对多关系定义Set吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the Grails domain classes Child and Toy. A child can have many toys.

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

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

据我了解,儿童"号上将有一个玩具物业.也就是说,将有Child方法:

It was my understanding that there will be a toys property on Child. That is, there will be the Child method:

public Set getToys()

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

But this doesn't work. I'm unable to reference child.getToys() from a Java class. I have explicitly define toys in Child:

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

这是正确的吗?我是否需要为一对多关系明确定义一个集合?

Is this correct? Do I need to explicitly define a Set for a one-to-many relationship?

推荐答案

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

Yes you have to expicitly define the property if you want to reference it from Java. This has to do with the way the groovy compiler (groovyc) creates the stub classes for the groovy beans.

这篇关于在Grails中,需要为一对多关系定义Set吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆