Home > Mobile >  How to set Id of one entity to the Id of another entity using JPA?
How to set Id of one entity to the Id of another entity using JPA?

Time:05-04

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. Following is my User entity:

@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 :

@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) . I don't want to add User class object in HealthData. I thought of using @OneToOne in HealthData but then it would add User in it. How can i just include Id from parent table in child table?

CodePudding user response:

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. 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. Allowing references to be marked as IDs is known as a derived ID and supported I believe since 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:

@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.

  • Related