基本上这是做你想做的事的方式。
员工
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
@Id
@Column(name = "EMP_ID")
private Long id;
@Column(name = "EMP_FNAME")
private String firstName;
@Column(name = "EMP_LNAME")
private String lastName;
@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL,
orphanRemoval = true)
private EmployeeRemark employeeRemark;
public void setRemark(String remark) {
this.employeeRemark = new EmployeeRemark();
this.employeeRemark.setRemark(remark);
this.employeeRemark.setEmployee(this);
}
public String getRemark() {
return employeeRemark == null ? null : employeeRemark.getRemark();
}
//getters and setters
}
员工备注
@Entity
@Table(name = "EMPLOYEE_REMARK")
public class EmployeeRemark implements Serializable {
@Id
@Column(name = "EMP_REMARK_ID")
private Long id;
@OneToOne
@JoinColumn(name = "EMP_ID")
private Employee employee;
@Column(name = "REMARK")
private String remark;
//getters and setters
}
保存员工时,只需调用保存员工即可。EmployeeRemark 将级联到所有操作,并将与员工一起被删除,或者如果它以其他方式成为孤儿。