Hibernate中“insertable = false”和“transient”之间的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate中“insertable = false”和“transient”之间的区别相关的知识,希望对你有一定的参考价值。

以下代码使用@Column注释和insertable=false

@Entity
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "User_Id")
    private int userId;

    @Column(name = "User_Name", insertable = false)
    private String userName;
}

以下代码使用@Transient注释。

@Entity
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "User_Id")
    private int userId;

    @Transient
    private String userName;
}

在这两种情况下,都不会创建列。

两个示例代码之间是否有任何不同的功能?

答案

@Transient的意思是:这个属性根本不是持久的。它不是由JPA处理的。每次从数据库中获取实体时,该属性将为null(或者由无参数构造函数初始化的任何属性)。

insertable=false表示JPA在保存实体时不会在insert语句中包含该列。但它会在更新实体时,它将从数据库加载它。

以上是关于Hibernate中“insertable = false”和“transient”之间的区别的主要内容,如果未能解决你的问题,请参考以下文章

一口一口吃掉Hibernate——Hibernate中inverse的用法

Hibernate学习———— hibernate中查询方式详解

Hibernate学习11——Hibernate 高级配置

Hibernate框架Hibernate的一级缓存

Hibernate框架Hibernate的一级缓存

hibernate下载包中配置文件路径