Grails数据绑定 - 无法修改不可变实体 [英] Grails data binding - Cannot make an immutable entity modifiable

查看:79
本文介绍了Grails数据绑定 - 无法修改不可变实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails 2.1.0上,我试图动态更新域类上的字段。该对象被绑定并且看起来很好,直到调用save方法,该方法抛出以下异常:



java.lang.IllegalStateException:无法修改不可变实体。



 尝试{
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
}

我试过使用直接分配绑定该领域,并且运作良好。

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

但是我需要为每种情况处理类型转换(我假设)。我需要的是BindDynamicMethod为我处理绑定。当使用BindDynamicMethod进行绑定时,对象发生什么变化是不可变的。或者我做错了什么导致它?



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



部分解决



事实证明,这是发生在一些域上的,但是一些在其映射中使用缓存的情况引发了这种异常。

  class UploadSettings {
String profile =default
字符串displayName
字符串名称
字符串值
字符串defaultValue

静态映射= {
//缓存使用情况:'只读'

}
}

所以我想现在我的问题是如果一个域使用缓存,为什么不能更新它的值?或者我们该怎么做?有没有办法来捕捉,如果域是不可变的?



谢谢

解决方案

是的,通过将其设置为只读,您将使对象不可变,正如错误所示,恕我直言,这是误导性的,因为我们处于缓存的上下文中,但背后有一些基本原理。



如果您需要在域级别进行缓存,那么将其设置为读写方式应该可以做到这一点。 缓存使用情况


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

解决方案

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

这篇关于Grails数据绑定 - 无法修改不可变实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆