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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With