注意,实体类中要使用org.springframework.data.annotation.Transient
在写实体类时发现有加@Transient注解的
加在属性声明上,但网上有加到get方法上的;
1 serialization会忽略掉
Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。
为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient
2 不跟数据库表做映射 就是表中没有这个字段
@Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性.
在项目查了下,mongodb中确实没有此字段,因此也适用于mongodb
个人认为,可以用于一些计算值,或缓存值,如
http://blog.sina.com.cn/s/blog_4e64ae7a0106grty.html
spring源码
package org.springframework.data.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Marks a field to be transient for the mapping framework. Thus the property will not be persisted and not further
* inspected by the mapping framework.
*
* @author Oliver Gierke
* @author Jon Brisbin
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Transient {
}