当关系定义为列表

时间:2016-05-11 09:25:16

标签: json grails

Grails new json-views和一对多关联存在问题。以下代码正在运行:

Class Foo:

class Foo {

    String name 

    static hasMany = [bars:Bar]

    static constraints = {
    }

}

班级栏:

class Bar {

    String name
    Foo foo

    static constraints = {
    }

}

fson(show.gson)的Json视图:

model {
    Foo foo
}

json {
    foo g.render(template:"foo", model:[foo:foo])
}

_foo.gson template:

model {
    Foo foo
}

json {
    name foo.name
    bars foo.bars.collect { it.id }
}

但是如果我改变foo类来使用List而不是这样:

class Foo {

    String name

    List bars

    static hasMany = [bars:Bar]

    static constraints = {
    }
}

我收到以下错误:

  

[静态类型检查] - 没有这样的属性:类的id:java.lang.Object    @第13行,第29栏。          bars foo.bars.collect {it.id}

我确信有办法解决这个问题,但我想知道是否有人经历过这个,并且知道为什么在使用List时它不起作用。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

正如https://github.com/grails/grails-views/issues/38上的问题所述,该集合需要声明为List<Bar> bars,而不是List bars