为什么JPA不尊重OneToOne中的外键?

时间:2018-01-04 14:04:21

标签: hibernate jpa foreign-keys relationship one-to-one

我曾与其他ORM合作,如Doctrine。现在我正在使用JPA和Spring Data。

在OneToOne的关系中我知道每一行必须与另一个表中只有一行有关系,好的!如果它是正确的谁可以帮助我并向我解释这个?

来自phpmyadmin的屏幕截图

联系人

contacts table

员工 employees table

这是联系对象

@Entity
@Table(name="contacts")
public class Contact {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String email;
    private String phone;
    @OneToOne(fetch=FetchType.EAGER)
    private Employee employee;
    public Contact() {}
    //getters& setters
}

这是员工对象

@Entity
@Table(name="employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String firstName;
    private String lastName;
    @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Contact contact;
    @OneToOne(fetch=FetchType.LAZY)
    private Address address;
    private Date created_at;
    private Date updated_at;
    public Employee() {}
    //getters& setters
}

0 个答案:

没有答案