Grails data binding - Cannot make an immutable entity modifiable

坚强是说给别人听的谎言 提交于 2019-12-13 19:13:48

问题


On a Grails 2.1.0 I am trying to dynamically updating a field on a domain class. The object gets binded and it looks fine, until the save method is called, which throws the following exception:

java.lang.IllegalStateException: Cannot make an immutable entity modifiable.

    try {
        def bindParams = [:]
        bindParams."$paramsFieldName" = "$paramsValue"
        def domainClass = grailsApplication.domainClasses.find { it.clazz.simpleName == paramsDomain }.clazz
        def objectInstance = domainClass.findById(paramsId)
        objectInstance."$paramsFieldName" = "$paramsValue"
        bindData(objectInstance, bindParams)
        objectInstance.save(flush:true ,failOnError:false)
        return objectInstance
    }
    catch (Exception ex) {
        log.error ex
        return null
    }

I tried to bind the field using direct assigment and worked well.

objectInstance."$paramsFieldName" = convertToType( fieldType.name,paramsValue)

but then I need to handle the type conversion for each case (I assume). What I need is the BindDynamicMethod handles the binding for me. What happens to the object when binding it using the BindDynamicMethod that makes is immutable?. Or what am I doing wrong that is causing it?

=========================================================

PARTIALLY SOLVED

It turned out that this was happening on some of the domains, but some that were using cache on their mapping was throwing this exception.

class UploadSettings {
    String profile = "default"
    String displayName
    String name 
    String value 
    String defaultValue 

    static mapping = {
        //cache usage:'read-only'

    }
}

So I guess now my question is if a domain is using cache , why cant we update its value? Or how can we do that? Is there a way to capture if the domain is immutable?

Thanks


回答1:


Yes by setting it to read-only you are making the object immutable as the error says, IMHO this is misleading as we are in the context of caching but there is some rationale behind this.

If you need caching at the domain level then setting it to read-write should do the trick

See cache usages



来源:https://stackoverflow.com/questions/12699393/grails-data-binding-cannot-make-an-immutable-entity-modifiable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!