Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @Transient annotation mean for methods?

So I have learned that the transient keyword in Java means that an entity does not persist, and that the @Transient annotation in JPA means don't persist a field to the database. But what does it mean when @Transient is applied to a method rather than a variable?

This is where I found it in our code:

@Transient
public boolean getTabFoo() {
    if ((this.viewFoo1 != ACCESS_NONE)
            || (this.viewFoo2 != ACCESS_NONE) || (this.viewFoo3 != ACCESS_NONE)
            || (this.getViewFoo4() != ACCESS_NONE)) {
        return true;
    }
    return false;
}
like image 299
starsplusplus Avatar asked Jan 31 '14 09:01

starsplusplus


People also ask

What does @transient annotation do?

Annotation Type Transient. This annotation specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class. Example: @Entity public class Employee { @Id int id; @Transient User currentUser; ... }

What does @transient mean in Java?

Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.

What does @transient mean in spring?

1. What is @Transient annotation in Spring? @Transient annotation is used to mark a field to be transient for the mapping framework, which means the field marked with @Transient is ignored by mapping framework and the field not mapped to any database column (in RDBMS) or Document property (in NOSQL).

What's the purpose of transient variables?

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.


1 Answers

All field-level JPA annotations can be placed either on fields or on properties, it determines access type of the entity (i.e. how JPA provider will access fields of that entity - directly or using getters/setters).

Default access type is determined by placement of @Id annotation, and it should be consistent for all fields of the entity (or hiererchy of inherited entities), unless explicitly overriden by @Access for some fields.

So, @Transient on getters has the same meaning as @Transient on fields - if default access type for your entity is property access, you need to annotate all getters that don't correspond to persistent properties with @Transient.

like image 114
axtavt Avatar answered Sep 28 '22 00:09

axtavt