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

问题描述 投票:0回答:1

我有Employee课程和Qualification课程,我成功地添加了员工的资格。但是,当我尝试通过增加一个资格来更新特定的员工资格时。我没有想法。坚持提出一些看法

员工班

@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;
}
}

资格课程

@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;
}
}

实施课

     //      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();
java hibernate spring-mvc one-to-many hibernate-onetomany
1个回答
0
投票

当你有双向关系时,你需要连接双方。在您的示例中,您已经拥有:

q1.add(newQ1);

但你也需要做反向绑定:

newQ1.setEmployee(emp)

请注意:您的员工与资格证书之间的关系(oneToMany和ManyToOne)都有Cascade.ALL。我没有运行你的代码,但我很确定会产生一个问题。您必须决定哪个实体负责更新另一个实体。 (i,如果您选择保存资格并将更改传播给员工,则从Employee类中的@oneToMany中删除级联

© www.soinside.com 2019 - 2024. All rights reserved.