鉴于此image,我想知道在我在其他文件中使用JoinColumn
的同时是否应该使用mappedBy
。或者,如果我不需要使用mappedBy
,那就没关系!
有人可以告诉我为什么我们需要使用mappedBy
?
Employee.java
@Id
@GeneratedValue
@Column(name="employee_id")
private Long employeeId;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@Column(name="birth_date")
private Date birthDate;
@Column(name="cell_phone")
private String cellphone;
@ManyToOne
@JoinColumn(name="department_id")
private Department department;
public Employee() {
}
public Employee(String firstname, String lastname, String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = new Date(System.currentTimeMillis());
this.cellphone = phone;
}
// Getter and Setter methods
Department.java
@Id
@GeneratedValue
@Column(name="DEPARTMENT_ID")
private Long departmentId;
@Column(name="DEPT_NAME")
private String departmentName;
@OneToMany(mappedBy="department")
private Set<employee> employees;
// Getter and Setter methods
答案 0 :(得分:2)
为什么你需要使用&#34; mappedBy&#34; ?您可以使用它来定义关系是双向的并指定关系的另一侧(对于1-1,您在FK的一侧指定它,对于1-N,您在Collection侧指定它)。所以你不要去&#34;如果您的关系是UNIDIRECTIONAL,请指定它。阅读任何一半体面的JPA文档会告诉你;您的实施文档是否告诉您这个?