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

时间:2019-03-21 12:24:39

标签: java hibernate spring-mvc one-to-many hibernate-onetomany

我有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();

1 个答案:

答案 0 :(得分:0)

当您具有双向关系时,需要将两侧都连接起来。在您的示例中,您已经有以下内容:

q1.add(newQ1);

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

newQ1.setEmployee(emp)

仅需注意:员工与资格之间的两个关系(oneToMany和ManyToOne)都具有Cascade.ALL。我尚未运行您的代码,但我很确定会造成问题。 您必须确定哪个实体负责更新另一个实体。 (即,如果您选择保存资格和要传播给员工的更改,则从Employee类的@oneToMany中删除级联