如何避免 'Cannot read write-only property' 在瞬态上使用可绑定约束

How to avoid 'Cannot read write-only property' using bindable constraint on transient

我在使用 grails 3.1.4 使用瞬态 属性 绑定值时遇到问题。

以该域名为例:

class Domain {
    Boolean b1
    Boolean b2
    Boolean b3

    void setPropertyList(propertyList) {
        if(propertyList.contains('someValue'))
            this.b1 = true         
    }

    static transients = ['propertyList']

    static constraints = {
        propertyList bindable: true
    }
}

我想使用特定的 属性(此处:propertyList)进行数据绑定。此 属性 在数据绑定源中可用,但在我的域中不可用。所以我添加了一个瞬态和一个 setter。为了包含用于数据绑定的瞬态 propertyList,我添加了 bindable 约束。

setter setPropertyList 在数据绑定时被调用。结果域实例的属性具有按预期设置的所有属性。 但是当我尝试保存结果实例时,出现以下异常:

groovy.lang.GroovyRuntimeException: Cannot read write-only property: propertyList
    at org.grails.validation.ConstrainedPropertyBuilder.doInvokeMethod(ConstrainedPropertyBuilder.java:74)

看起来 grails 在验证实例时遇到了一些问题。

有什么解决办法吗?

经过一些调试后发现,grails 找不到 propertyList 的类型,因此跳过了数据绑定。

添加 getter 有助于 grails 推断类型。 这样就避免了异常。

List<String> getPropertyList() {}