使用 OnetoMany 关系从父 class 更新子 class 实体

Update a child class entity from parent class using OnetoMany relationship

我有员工 class 和资格 class ,我成功添加了一名员工的资格。但是,当我尝试通过再添加一项资格来更新特定员工的资格时。我不知道 do.Kindly 建议一些观点

Employee class

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

private int empId;
private String empName;
private Employee_Address addressDetail;
private List<Employee_Qualification> qualifications;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="EmployeeId", updatable = false, nullable = false)
public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}


@Column(name="EmployeeName")
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}


@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="EmpAdd_FK")
public Employee_Address getAddressDetail() {
    return addressDetail;
}
public void setAddressDetail(Employee_Address addressDetail) {
    this.addressDetail = addressDetail;
}


@OneToMany(targetEntity=Employee_Qualification.class, mappedBy="employee"
        ,cascade=CascadeType.ALL, fetch=FetchType.LAZY)
public List<Employee_Qualification> getQualifications() {
    return qualifications;
}
public void setQualifications(List<Employee_Qualification> qualifications) {
    this.qualifications = qualifications;
}
}

Qualification class

@Entity
@Table (name="Tbl_Employee_Qualification")
public class Employee_Qualification {

private int qualificationId;
private String qualification;
private Employee employee;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="QualificationId", updatable = false, nullable = false)
public int getQualificationId() {
    return qualificationId;
}
public void setQualificationId(int qualificationId) {
    this.qualificationId = qualificationId;
}

@Column(name="Qualifications")
public String getQualification() {
    return qualification;
}
public void setQualification(String qualification) {
    this.qualification = qualification;
}

@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="Emp_FK")
public Employee getEmployee() {
    return employee;
}
public void setEmployee(Employee employee) {
    this.employee = employee;
}
}

Implementation class

     //      Update Employee and Employee_Qualification from Employee entity class [OnetoManny and ManytoOne bidirectional]   
     Employee emp =(Employee) session.createQuery("from Employee where empId='10'").uniqueResult();
    Employee_Qualification  newQ1  = new Employee_Qualification();
    newQ1.setQualification("ECE");

    List<Employee_Qualification> q1 = emp.getQualifications(); 
    q1.add(newQ1);

    emp.setQualifications(q1);
    session.save(q1);
    session.getTransaction().commit();

当你有双向关系时,你需要连接双方。在你的例子中你已经有了这个:

q1.add(newQ1);

但您还需要进行反向绑定:

newQ1.setEmployee(emp)

请注意:您的员工和资格之间的两种关系(oneToMany 和 ManyToOne)都具有 Cascade.ALL。我没有 运行 你的代码,但我很确定这会造成问题。 您必须决定哪个实体负责更新另一个实体。 (即,如果您选择保存资格和要传播给员工的更改,则从员工 class

中的 @oneToMany 中删除级联