使用 OnetoMany 关系从父类更新子类实体
Posted
技术标签:
【中文标题】使用 OnetoMany 关系从父类更新子类实体【英文标题】:Update a child class entity from parent class using OnetoMany relationship 【发布时间】:2019-03-21 12:24:39 【问题描述】:我有 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();
【问题讨论】:
父类不应该知道子类的存在。请注意:您显示的是组合,而不是继承,您的代码中没有父/子关系。 @Stultuske 在谈论数据库时,将一对多关系称为父子关系是很常见的。由于我主要在 OOP 世界中工作,因此我也一直并且偶尔仍然对此感到困惑。 【参考方案1】:当你有一个双向关系时,你需要连接两边。在您的示例中,您已经有了:
q1.add(newQ1);
但你也需要做反向绑定:
newQ1.setEmployee(emp)
请注意:您对员工和资格之间的两种关系(oneToMany 和 ManyToOne)都有 Cascade.ALL。我还没有运行你的代码,但我很确定会产生问题。 您必须决定哪个实体负责更新另一个。 (即,如果您选择保存要传播给员工的资格和更改,则从 Employee 类中的@oneToMany 中删除级联
【讨论】:
谢谢你,我做了你提到的同样的事情,它的工作以上是关于使用 OnetoMany 关系从父类更新子类实体的主要内容,如果未能解决你的问题,请参考以下文章
java子类从父类继承某个属性,怎么添加特定的注解不影响父类
在JPA双向@OnetoMany关系中,当我更新父实体时,子实体在数据库中被删除