Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't initialize a transient member?

Tags:

grails

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?

like image 276
goool Avatar asked Jun 24 '12 06:06

goool


People also ask

How do you serialize a transient variable in Java?

transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

Why are transient variables not serialized?

In case you define any data member as transient, it will not be serialized. This is because every field marked as transient will not be serialized. You can use this transient keyword to indicate the Java virtual machine (JVM) that the transient variable is not part of the persistent state of an object.

What will happen to transient variable after deserialization?

With Transient variable we can stop serializing the required values, but after deserialization we are getting default values of transient variables and we are loosing the original values. So then what is the need of creating transient variable instead we can skip creating the variable itself.

How do you make a class transient in Java?

Example of Java Transient Keyword The age data member of the Student class is declared as transient, its value will not be serialized. If you deserialize the object, you will get the default value for transient variable. Let's create a class with transient variable. Now write the code for deserialization.


1 Answers

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.

like image 171
Ian Roberts Avatar answered Sep 23 '22 05:09

Ian Roberts