简体   繁体   English

如何使用 JPA 将一个实体的 ID 设置为另一个实体的 ID?

[英]How to set Id of one entity to the Id of another entity using JPA?

I'm new at Spring Boot's JPA concept so need your help in deciding how to import just the ID of another entity, say User into HealthData entity.我是 Spring Boot 的 JPA 概念的新手,因此在决定如何仅将另一个实体的ID导入时需要您的帮助,比如将User导入HealthData实体。 Following is my User entity:以下是我的User实体:

@Entity
@Table(name = "user",uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;
    @Email
    @Column(nullable = false)
    private String email;

    private String imageUrl;
    @Column(nullable = false)
    private Boolean emailVerified=false;
    @JsonIgnore
    private String password;
    @NonNull
    @Enumerated(EnumType.STRING)
    private AuthProvider authProvider;
    private String providerId;
}

And I wish to define HealthData entity in the following manner :我希望以下列方式定义HealthData实体:

@Entity
@Table(name = "HealthData",uniqueConstraints = {@UniqueConstraint(columnNames = "id")})
public class HealthData {
    @Id
    private Long id; //how to import id of User here?

    @Column
    private Double height;
    @Column
    private Double weight;
    @Column
    private int age;
    ...other columns

}

Now, I wish to use Id of User to this entity(kind of making parent-child relationship) .现在,我希望将User Id用于该实体(建立父子关系的一种)。 I don't want to add User class object in HealthData.我不想在 HealthData 中添加User类对象。 I thought of using @OneToOne in HealthData but then it would add User in it.我曾想过在HealthData中使用@OneToOne ,但随后会在其中添加User How can i just include Id from parent table in child table?我怎样才能在子表中包含父表中的 ID?

In this case, your HealthData has a reference to User, and I'm not sure why you wouldn't have mapped this as a foreign key.在这种情况下,您的 HealthData 具有对 User 的引用,我不确定您为什么不将其映射为外键。 If you are able to do so, I'd suggest the following:如果您能够这样做,我建议您执行以下操作:

@Entity
@Table(name = "HealthData")
public class HealthData {
    @Id
    @OneToOne
    @JoinColumn(name = "id")
    private User user; 

    @Column
    private Double height;
    @Column
    private Double weight;
    @Column
    private int age;
    ...other columns
}

JPA then handled setting the "ID" to the value within your user instance for you, and can persist both in the same transaction automatically.然后,JPA 为您处理将“ID”设置为您的用户实例中的值,并且可以自动将两者保留在同一事务中。 Allowing references to be marked as IDs is known as a derived ID and supported I believe since JPA 2.0.允许将引用标记为 ID 被称为派生 ID,并且我相信自 JPA 2.0 以来就受支持。

As for efficiency, you can still lazy fetch or even not fetch the user instance.至于效率,您仍然可以延迟获取甚至不获取用户实例。 It is simple to just map the ID column as a basic using a slightly different approach:使用稍微不同的方法将 ID 列映射为基本列很简单:

@Entity
@Table(name = "HealthData")
public class HealthData {
    @Id
    private Long id;
    
    @MapsId
    @OneToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "id")
    private User user; 

    @Column
    private Double height;
    @Column
    private Double weight;
    @Column
    private int age;
    ...other columns
}

JPA will set both the User id as well as the healthData.id values based on what it generates for the user Id sequence when you set the healthData.user reference.当您设置 healthData.user 引用时,JPA 将根据它为用户 ID 序列生成的内容来设置用户 ID 和 healthData.id 值。

可以使用getter和setter来设置healthdata表中user id的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM