在@ManyToOne侧合并托管实体 [英] Merging a managed entity on the @ManyToOne side

查看:122
本文介绍了在@ManyToOne侧合并托管实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部门员工

下给出一对多关系。

部门(家长):

  @OneToMany(mappedBy =department,fetch = FetchType。 LAZY,cascade = CascadeType.ALL)
私人列表< Employee> employeeList = new ArrayList< Employee>(0);

员工(子女):

  @JoinColumn(name =department_id,referencedColumnName =department_id)
@ManyToOne(fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH ,CascadeType.DETACH})
私人部门部门;

合并如下的托管实体(子项)(在EJB中使用CMT),

  Employee employee = entityManager.find(Employee.class,1L); 
employee.setDepartment(department); //部门由客户提供。
employee.setEmployeeName(xyz);
entityManager.merge(employee);

不会更新数据库表中相应的员工行。只有当 CascadeType.MERGE 从子$ @ManyToOne 关系中< Employee 。



为什么表中的行没有被更新? CascadeType.MERGE 关于这个例子的唯一目的是什么?



我目前使用EclipseLink 2.6.0 JPA 2.1。

解决方案

级联应始终从父级传播到子级而不是相反。



在你的情况下,你需要从子端删除Cascade:

  @JoinColumn(name =department_id ,referencedColumnName =department_id)
@ManyToOne(fetch = FetchType.LAZY)
私人部门部门;

并确保您设置关联的双方:

  Employee employee = entityManager.find(Employee.class,1L); 
employee.setDepartment(department);
employee.setEmployeeName(xyz);
department.getEmployeeList()。add(employee);
entityManager.merge(department);


Given below a one-to-many relationship from Department to Employee.

Department (parent) :

@OneToMany(mappedBy = "department", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Employee> employeeList = new ArrayList<Employee>(0);

Employee (child) :

@JoinColumn(name = "department_id", referencedColumnName = "department_id")
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
private Department department;

Merging a managed entity (child) like the following (using CMT in EJB),

Employee employee = entityManager.find(Employee.class, 1L);
employee.setDepartment(department); // department is supplied by a client.
employee.setEmployeeName("xyz");
entityManager.merge(employee);

does not update the corresponding employee row in the database table. It happens only when CascadeType.MERGE is removed from the child @ManyToOne relationship in Employee.

Why doesn't the row in the table get updated? What is the sole purpose of CascadeType.MERGE regarding this example?

I am currently using EclipseLink 2.6.0 having JPA 2.1.

解决方案

Cascading should always propagate from a Parent to a Child and not the other way around.

In your case, you need to remove the Cascade from the child-side:

@JoinColumn(name = "department_id", referencedColumnName = "department_id")
@ManyToOne(fetch = FetchType.LAZY)
private Department department;

and make sure you set both sides of the association:

Employee employee = entityManager.find(Employee.class, 1L);
employee.setDepartment(department);
employee.setEmployeeName("xyz");
department.getEmployeeList().add(employee);
entityManager.merge(department);

这篇关于在@ManyToOne侧合并托管实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆