JPA CascadeType.REMOVE无法正常工作 [英] JPA CascadeType.REMOVE not working

查看:147
本文介绍了JPA CascadeType.REMOVE无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体 Business ,它们由部门

I have two entities Business which is composed of a list of Departments

@Entity
@Table(name = "Business")
public class Business implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "Id")
  private Long id;

  @OneToMany(mappedBy = "business", 
       cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
   private List<Department> departments;

   @OneToMany(mappedBy = "business", orphanRemoval = true, 
     cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
   private List<Process> processs;

   @ManyToMany
   private List<Competence> competences;
}


@Entity
@Table(name = "Department")
public class Department implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   @OneToMany(mappedBy = "father", 
        cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
   private List<Department> departments;
}

当我尝试删除业务实例时,出现Mysql异常

When I try to remove a business instance I get a Mysql Exception

无法删除或更新父行:外键约束失败(evac_java.Department,CONSTRAINT FK_Department_Business外国密钥(Business)参考Business(Id)): HY000-空

Cannot delete or update a parent row: a foreign key constraint fails (evac_java.Department, CONSTRAINT FK_Department_Business FOREIGN KEY (Business) REFERENCES Business (Id)):HY000 - null

这意味着我不能删除该业务实例,因为它具有与之关联的部门,但是部门本身不能存在,因此我想在删除所有业务部门时将其删除.我以为可以通过在业务实体中的@OneToMany批注中添加cascade = CascadeType.REMOVE来实现此目的,但是它不起作用.

Which means I can't delete the business instance because it has departments associated with it, but a department cannot exists by itself so I want to delete all business's departments when it gets removed. I thought I would achieve this by adding cascade = CascadeType.REMOVE to the @OneToMany annotation in the business entity, but it does not work.

我在网上进行了搜索,结果在stackoverflow上发现了很多与此类似的问题,但是它们都提出了相同的问题: add Cascade = CascadeType.REMOVE或CascadeType.ALL

I did a search on the net and I found a lot of questions similar to this one on stackoverflow but they all suggest the same: add cascade = CascadeType.REMOVE or CascadeType.ALL

所以我想知道我是否错过了某事.

So I'm wondering if I'm missing somethig.

我正在使用Glassfish 4.1和EclipseLink

I'm using Glassfish 4.1 and EclipseLink

我尝试过

@OneToMany(mappedBy = "business", orphanRemoval = true)
private List<Department> departments;

在企业实体上,但它也不起作用

on the business entity but it does not work either

这是我用来删除在抽象类中声明的实体的方法

Here's the method I'm using to remove entities which is declared in an abstract class

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
}

推荐答案

JPA只能删除并级联它所了解的实体,如果您尚未维护双向关系的双方,则会出现类似问题.如果部门集合为空,请在删除之前尝试使用em.refresh(),强制JPA填充所有关系,以便可以正确删除它们,但是最好保持关系的双方,因为要进行更改以避免数据库命中.

JPA can only remove and cascade the remove over entities it knows about, and if you have not been maintaining both sides of this bidirectional relationship, issues like this will arise. If the collection of departments is empty, try an em.refresh() before the remove, forcing JPA to populate all relationships so that they can be correctly removed, though it is better to maintain both sides of the relationship as changes are made to avoid the database hit.

这篇关于JPA CascadeType.REMOVE无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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