How to link foreign key between @Entity in spring boot data jpa

The exception shows that when you try to save User it saves null as facilityId.

To make this work you need to specify the Facility as another table in User entity (you did it wrong way).

@Table(name= "user_detail")
public class User {

    private Integer id;
    private String username;
    @Column (name="contactNo")
    private String contactNo;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "facilityId", referencedColumnName = "id")
    private Facility facility;
    //getter and setter

Based on your requirement you need to specify the relationship. It seems here that the relationship should be one to one so I marked it as such.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top