@Transient 注解用于标记实体类中的字段
,表示该字段不会持久化到数据库
。当实体类中的某个字段使用了 @Transient 注解时,JPA实现将会忽略该字段,不会将其保存到数据库表中。这通常用于标记一些临时或计算得到的字段,这些字段不需要被持久化。
代码如下:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@Transient
private transient String tempData;
}
在上面的示例中, Product 实体类中的 tempData 字段被标记为 @Transient ,表示这个字段不会被持久化到数据库。该字段被声明为 transient ,以确保在序列化过程中也不会被持久化。