0
votes

I have two different tables, Person table and Employee table. I need a one-to-one mapping between these two. Employee table's emp_id references Person table's person_id. I need some help in writing the mapping using annotations

persons.java

@OneToOne(mappedBy="persons1", cascade=CascadeType.ALL) public Employee getemployee() {

EMPLOYEE.JAVA

@ManyToOne @JoinColumn(name = "PERSON_ID")

1
If it's a one-to-one association, why would you use @ManyToOne?JB Nizet
I read in a post, for bidirectional One - one mapping you use Many to oneAmrita Sharma

1 Answers

0
votes

Use mappedBy when you need a bidirectional relationship. It will be better to first understand the Hibernate relationship and entity modelling.

@Entity
@Table("person")
public class Person{

@OneToOne(cascade=CascadeType.ALL) 
public Employee getEmployee(){

}

}

    @Entity
    @Table("employee")
    public class Employee{

    @OneToOne 
    public Person getPerson(){

    }

}