grails - 如何在 transient 上使用可绑定(bind)约束来避免 'Cannot read write-only property'

标签 grails grails-orm grails-3.1

我在使用 grails 3.1.4 使用 transient 属性绑定(bind)值时遇到问题。

以这个域为例:

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)进行数据绑定(bind)。此属性在数据绑定(bind)源中可用,但在我的域中不可用。所以我添加了一个 transient 和一个二传手。包括 transient propertyList对于数据绑定(bind),我添加了 bindable约束。

二传手setPropertyList在数据绑定(bind)期间调用。结果域实例的属性按预期设置了所有属性。
但是当我尝试保存结果实例时,我得到以下异常:
groovy.lang.GroovyRuntimeException: Cannot read write-only property: propertyList
    at org.grails.validation.ConstrainedPropertyBuilder.doInvokeMethod(ConstrainedPropertyBuilder.java:74)

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

任何想法如何解决这一问题?

最佳答案

经过一番调试,结果表明 grails 找不到 propertyList 的类型。因此跳过数据绑定(bind)。

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

List<String> getPropertyList() {}

关于grails - 如何在 transient 上使用可绑定(bind)约束来避免 'Cannot read write-only property',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469320/

相关文章:

grails - 我怎样才能有一个提交按钮(grails)来更新一个特定的目标框架?

grails - hasMany 保存不适用于 Grails 2.4.2 升级

Grails 3 拦截器并呈现自定义 JSON View

grails - Grails 3.1.4无法运行

grails - Grails 3 Fontawesome设置

grails - 如何编写Grails过滤器的集成测试

javascript - 使用 onblur 来相乘 <g :field> grails

hibernate - grails 2.4.2 乐观锁定似乎不起作用

postgresql - Heroku Postgres : This connection has been closed

grails - 常见的添加方法