Steve Kuo
Steve Kuo

Reputation: 63094

In Grails, need to define a Set for one-to-many relationship?

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
}

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

public Set 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?

Upvotes: 2

Views: 1238

Answers (1)

Siegfried Puchbauer
Siegfried Puchbauer

Reputation: 6539

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.

Upvotes: 3

Related Questions