我曾与其他ORM合作,如Doctrine。现在我正在使用JPA和Spring Data。
在OneToOne的关系中我知道每一行必须与另一个表中只有一行有关系,好的!如果它是正确的谁可以帮助我并向我解释这个?
来自phpmyadmin的屏幕截图
联系人
员工
这是联系对象
@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
}