transient
字面意思:adj. 短暂的; 片刻的; 转瞬即逝的
使用场景
一个对象如果实现了Serilizable接口,就表示这个对象可以被序列化。
有时候我们会有这样的需求,需要实体类中的某几个字段(属性值)用来记录一些瞬时的值(也就是只仅在内存中操作),但是序列化到磁盘(也就是数据库),或者在网络传输序列化时,不希望这些字段被记录或传输,这时候我们就可以在这些字段上面加上transient关键字。
@Trasient注解
当我们使用Hibernate-jpa的时候,我们会使用@Transient注解来标记我们想要忽略的字段或元素。这个注解的作用和transient关键字效果一样。
package javax.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 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.
* 表示字段或者属性是不可持续的。
* 用来注解一个实体类的属性或者字段,映射的超类,内部类
*
* <pre>
* Example:
*
* @Entity
* public class Employee {
* @Id int id;
* @Transient User currentUser;
* ...
* }
* </pre>
*
* @since Java Persistence 1.0
*/
@Target({ METHOD, FIELD })//
@Retention(RUNTIME)//决定注解生命周期的注解,不加默认是CLASS
public @interface Transient {
}
定义注解时用到的注解
@Target注解
package java.lang.annotation;
/**
* Indicates the kinds of program element to which an annotation type
* is applicable. If a Target meta-annotation is not present on an
* annotation type declaration, the declared type may be used on any
* program element. If such a meta-annotation is present, the compiler
* will enforce the specified usage restriction.
* 表示这类程序元素上可以使用这种注解类型。例如:ElementType.METHOD
* 如果Target元注解没有在一个注解类型申明上出现,那么声明的(注解)类型可以被任意程序元素使用。(例如,不可以在
* ElementType的任意元素上使用,而不仅仅是METHOD)。
* 如果使用了Target注解,编译器将强制限定使用这些约束(如限制只能在METHOD,FIELD上使用被@Target注解的这个注解元素)
*
*
* For example, this meta-annotation indicates that the declared type is
* itself a meta-annotation type. It can only be used on annotation type
* declarations:
* 例如,@Target用来限定下面的这个注解,只能在元素是ANNOTATION_TYPE的时候使用
* <pre>
* @Target(ElementType.ANNOTATION_TYPE)
* public @interface MetaAnnotationType {
* ...
* }
* </pre>
* This meta-annotation indicates that the declared type is intended solely
* for use as a member type in complex annotation type declarations. It
* cannot be used to annotate anything directly:
* 这里表示在复杂的注解类型申明中,只能作为成员类型时使用。 而不能被用作直接注解任何东西
* <pre>
* @Target({})
* public @interface MemberType {
* ...
* }
* </pre>
* It is a compile-time error for a single ElementType constant to
* appear more than once in a Target annotation. For example, the
* following meta-annotation is illegal:
* 单独一个ElementType常量不在@Target注解中出现多次。如下面的FIELD常量。
* <pre>
* @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
* public @interface Bogus {
* ...
* }
* </pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
ElementType 枚举类
package java.lang.annotation;
/**
* A program element type. The constants of this enumerated type
* provide a simple classification of the declared elements in a
* Java program.
* 一个程序元素类型。这个枚举类提供了一个Java程序中的申明元素的简单的分类
*
* <p>These constants are used with the {@link Target} meta-annotation type
* to specify where it is legal to use an annotation type.
* 这些常量结合@Target注解使用去限定被@Target注解的这个注解类在哪里使用是合法的。
*
* @author Joshua Bloch
* @since 1.5
*/
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE
}
@Retention
package java.lang.annotation;
/**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* <tt>RetentionPolicy.CLASS</tt>.
* 表示被注解的注解类可以留存多久。也就是生命周期的时间范围。
* 如果注解类型上不适用@Retention注解,
* 默认的retention政策是 RetentionPolicy.CLASS
*
* <p>A Target meta-annotation has effect only if the meta-annotated
* type is use directly for annotation. It has no effect if the meta-annotated
* type is used as a member type in another annotation type.
* @Target注解表示的范围ANNOTATION_TYPE是指的在@Retention注解被直接使用在注解类型上,
* 当被用作成员变量上的时候不受这个限制。
*
* @author Joshua Bloch
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
总结
1,被transient修饰的变量,无法被序列化。
2,数据库中有对应字段的变量不能用trasient修饰,只有用户自定义的变量可以,使用transient的类必须序列化。