【问题标题】:Cannot iterate an one to many relation in Grails json-views when relation is defined as list当关系定义为列表时,无法在 Grails json-views 中迭代一对多关系
【发布时间】:2016-05-11 09:25:15
【问题描述】:

我对 Grails 新的 json-views 和一对多关联有疑问。以下代码正在运行:

Foo 类:

class Foo {

    String name 

    static hasMany = [bars:Bar]

    static constraints = {
    }

}

类栏:

class Bar {

    String name
    Foo foo

    static constraints = {
    }

}

foo 的 Json 视图(show.gson):

model {
    Foo foo
}

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

_foo.gson 模板:

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 列。 酒吧 foo.bars.collect { it.id }

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

非常感谢您的帮助。

【问题讨论】:

标签: json grails


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2013-09-19
    • 2016-06-05
    相关资源
    最近更新 更多