Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - Grails: setting transient fields in the map constructor

I'm trying to persist Maps of properties as single JSON-encoded columns, as shown in this question.

The problem I'm having is that apparently transient properties cannot be set in the default map constructor. Given any transient field:

class Test {
    //...
    String foo
    static transients = ['foo']
}

It seems that the map constructor (which Grails overrides in various ways) simply discards transient fields:

groovy:000> t = new Test(foo:'bar')
===> Test : (unsaved)
groovy:000> t.foo
===> null

While direct assignment (through the setter method) works as expected:

groovy:000> c.foo = 'bar'
===> bar
groovy:000> c.foo
===> bar

Is there a way to make the map constructor accept transient fields?


Or rather: is there a better way to persist a Map as a single JSON-encoded DB field, rather than the method shown in the linked question?

Here's the complete example:

import grails.converters.JSON

class JsonMap {
    Map data
    String dataAsJSON

    static transients = ['data']
    def afterLoad()      { data = JSON.parse(dataAsJSON) }
    def beforeValidate() { dataAsJSON = data as JSON }
}

I can set data using the setter (which will then be converted into dataAsJSON) but not using the map constructor.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The map constructor in GORM uses the data binding mechanism, and transient properties are not data-bindable by default. But you can override this using the bindable constraint

class Test {
    //...
    String foo
    static transients = ['foo']

    static constraints = {
        foo bindable:true
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...