Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between 'static transients' and 'transient Type aField' declaration for GORM?

Let us consider two Grails domain example classes.

1st class:

class Person {

    String name
    Integer counter = 0

    static transients = ['counter']
}

2nd class:

class Vehicle {

    String name
    transient Integer counter = 0
}

Will there be any difference in GORM persistence or domain class behaviour for the Integer counter field between classes Person and Vehicle?

EDIT: I know that Person class is the good way to do it as referenced by Grails docs. However I would prefer the Vehicle class way as it seems to be more obvious and easier not to overlook when reading a code.

like image 751
topr Avatar asked Sep 17 '12 15:09

topr


People also ask

When to use static and transient in a class?

If you have assigned a value to a variable while loading the class then that value will be assigned to the static variable while de-serializing the class but not to transient. So if you are using both of these modifiers with a variable, then in that sense, I am saying that static will take precedent to transient.

What is the difference between static and transient thermal analysis?

If the inertia and damping effects are not important, you might be able to use a static analysis instead. A transient thermal analysis follows basically the same procedures as a steady-state thermal analysis. The main difference is that most applied loads in a transient analysis are functions of time.

Is there any compile-time error when declaring final variable as transient?

However there is no compilation error. transient and final : final variables are directly serialized by their values, so there is no use/impact of declaring final variable as transient. There is no compile-time error though.

What is the use of transient in JVM?

When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type. transient keyword plays an important role to meet security constraints. There are various real-life examples where we don’t want to save private data in file.


1 Answers

The two mechanisms define different kinds of "transience". static transients defines bean properties that should not be mapped to the database by Hibernate, whereas the transient keyword denotes a field that should not be saved by the Java object serialization mechanism (e.g. when using webflow). They both have their uses in different situations.

like image 87
Ian Roberts Avatar answered Oct 24 '22 15:10

Ian Roberts