简体   繁体   中英

Can't initialize a transient member?

In grails 2.0.4, I hava a domain class like this:

class Foo {

    String pres
    String temp

    static transients = ['temp']

    def beforeInsert = {
        println "pres: ${pres}"
        println "temp: ${temp}"
    }
}

In BootStrap.groovy:

def f1 = new Foo(pres: "p1", temp: "t1")
f1.save()

def f2 = new Foo(pres: "p2")
f2.temp = "t2"
f2.save()

Then grails run-app, I got:

pres: p1
temp: null
pres: p2
temp: t2

What's the difference between f1 and f2, can't initialize a transient member?

The bindable constraint allows you to override the default behaviour. It would typically be used to disable data binding for a property that would normally be bindable by default, but I believe you can use it the other way too.

I ran into this same thing after upgrading to Grails 2. See these two JIRA entries if you want more info:

http://jira.grails.org/browse/GRAILS-8972

http://jira.grails.org/browse/GRAILS-9098

But, ultimately, I've had to resort to the same work around that you did in your example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM