Java有transient关键字。为什么JPA有@Transient而不是简单地使用已经存在的java关键字?


当前回答

通俗地说,如果在实体的属性上使用@Transient注释:该属性将被单独挑选出来,不会保存到数据库中。实体内对象的其余属性仍将被保存。

我保存对象到数据库使用jpa存储库内置的保存方法,这样:

userRoleJoinRepository.save(user2);

其他回答

I will try to answer the question of "why". Imagine a situation where you have a huge database with a lot of columns in a table, and your project/system uses tools to generate entities from database. (Hibernate has those, etc...) Now, suppose that by your business logic you need a particular field NOT to be persisted. You have to "configure" your entity in a particular way. While Transient keyword works on an object - as it behaves within a java language, the @Transient only designed to answer the tasks that pertains only to persistence tasks.

Java的transient关键字用于表示字段不被序列化,而JPA的@Transient注释用于表示字段不被持久化到数据库中,也就是说,它们的语义是不同的。

因为它们有不同的含义。@Transient注释告诉JPA提供者不要持久化任何(非瞬态的)属性。另一个告诉序列化框架不要序列化某个属性。您可能希望有一个@Transient属性并仍然序列化它。

正如其他人所说,@Transient用于标记不应该持久化的字段。请看这个简短的例子:

public enum Gender { MALE, FEMALE, UNKNOWN }

@Entity
public Person {
    private Gender g;
    private long id;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getId() { return id; }
    public void setId(long id) { this.id = id; }

    public Gender getGender() { return g; }    
    public void setGender(Gender g) { this.g = g; }

    @Transient
    public boolean isMale() {
        return Gender.MALE.equals(g);
    }

    @Transient
    public boolean isFemale() {
        return Gender.FEMALE.equals(g);
    }
}

当这个类被提供给JPA时,它持久化了性别和id,但不会尝试持久化helper布尔方法——如果没有@Transient,底层系统会抱怨实体类Person缺少setMale()和setFemale()方法,因此根本不会持久化Person。

对于Kotlin开发人员,请记住Java transient关键字将成为内置的Kotlin @Transient注释。因此,如果您在实体中使用JPA @Transient,请确保您有JPA导入:

import javax.persistence.Transient