skip to Main Content

Can someone give me a hand with mapping this scenario? I tried adding a Salary table and reference to the Instructor just to scratch my curiosity, but only the Details table populates the instructor_id column. The Salary table shows null as the instructor_id, so it doesn’t have a relation to the Instructor. How do I link both the details and the salary to the instructor using OneToOne relations? Here is what I tried. An explanation with the solution would be helpful as well. Thanks in advance.

@Entity(name = "instructor")
@Table(name = "instructor")
public class Instructor {
    @Id
    @Column(name = "id")
    private int id;

    @OneToOne(mappedBy= "instructor", cascade = CascadeType.ALL)
    @ToString.Exclude
    private Details details;
    
    @OneToOne(mappedBy= "instructor", cascade = CascadeType.ALL)
    @ToString.Exclude
    private Salary salary;
}

@Entity(name = "details")
@Table(name = "details")
public class Details {
    @Id
    @Column(name = "id")
    private int id;

    @JoinColumn(name = "id")
    @OneToOne
    private Instructor instructor;
}

@Entity(name = "salary")
@Table(name = "salary")
public class Salary {
    @Id
    @Column(name = "id")
    private int id;

    @JoinColumn(name = "id")
    @OneToOne
    private Instructor instructor;
}


      

2

Answers


  1. Chosen as BEST ANSWER

    this was happening because i wasnt setting the instructor in the salary object. i was setting it in the details object.

    details.setInstructor(instructor);
    salary.setInstructor(instructor);   <<<< the solution
    

  2. can I see the service implementation where you are adding a salary to the instructor?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search