Grails域类瞬态集合属性setter问题

时间:2014-10-18 17:00:03

标签: grails collections gorm getter-setter transient

我正在尝试将值设置(或绑定)到瞬态List属性。但我收藏失败了。 另一方面,瞬态String属性在setter上运行良好。

Grails版本2.4.3

有什么建议吗?

@Resource(uri = "/api/samples", formats = ["json"])
class Sample {

    static transients = ["fields","sample"]

    String regions
    String name
    String sample


    List<String> fields

    List<String> getFields() {
        this.regions != null ? Arrays.asList(regions.split("\\s*,\\s*")) : new ArrayList<String>();
    }

    void setFields(List<String> fields) {
        if (fields != null && !fields.isEmpty()) {
            this.regions = fields.join(",")
        }
    }

    void setSample(String sample){
        this.name = sample
    }

    static mapping = {
    }
}

1 个答案:

答案 0 :(得分:1)

默认情况下,无类型字段是瞬态的,因此这种替代方法应该有效(并且更加简洁):

@Resource(uri = "/api/samples", formats = ["json"])
class Sample {

    static transients = ["sample"]

    String regions
    String name
    String sample


    def getFields() {
        this.regions != null ? Arrays.asList(regions.split("\\s*,\\s*")) : []
    }

    void setFields(def fieldList) {
        if (fieldList) {
            this.regions = fieldList.join(",")
        }
    }

    void setSample(String sample){
        this.name = sample
    }

    static mapping = {
    }
}