@MapsId

Showing posts with label @MapsId. Show all posts
Showing posts with label @MapsId. Show all posts

The best way to map @OneToOne


@OneToOne

The best approach to bidirectional @OneToOne is to use @MapsId

Student entity

@Entity
@Data
@Table(name = "students")
public class Student implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String email;

    @OneToOne(mappedBy = "student", cascade = CascadeType.ALL)
    private StudentDetails studentDetails;
}

StudentDetails entity

@Entity
@Data
@Table(name = "student_details")
public class StudentDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String studentDetailsInfo;

    @OneToOne
    @JoinColumn(name = "student_id")
    @MapsId
    private Student student;
}