使用注解@Transient使表中沒有此字段
注意,實體類中要使用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 { }