Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 级联所有不删除_Java_Jpa_Jpa 2.0 - Fatal编程技术网

Java 级联所有不删除

Java 级联所有不删除,java,jpa,jpa-2.0,Java,Jpa,Jpa 2.0,请检查此实体: @Entity @Table(name = "css_empresa") public class Empresa extends EntidadContactable implements Serializable, Convert { private static final long serialVersionUID = 1L; @Id @SequenceGenerator(name = "EMPRESA_ID_GENERATOR", sequenceName =

请检查此实体:

@Entity
@Table(name = "css_empresa")
public class Empresa extends EntidadContactable implements Serializable,
    Convert {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name = "EMPRESA_ID_GENERATOR", sequenceName = ConstantesSecuencias.SEQ_EMPRESA, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMPRESA_ID_GENERATOR")
@Column(name = "cod_empresa", unique = true, nullable = false)
private Long id;


@Column(name = "num_ruc", precision = 13)
private BigDecimal numRuc;

@Column(name = "num_rup", precision = 15)
private BigDecimal numRup;

@Column(name = "txt_direccion_web", length = 255)
private String txtDireccionWeb;

@Column(name = "txt_nombre", nullable = false, length = 255)
private String txtNombre;

@Column(name = "txt_observaciones", length = 255)
private String txtObservaciones;

@OneToOne
@JoinColumn(name = "cod_usuario")
private Usuario administrador;

// bi-directional many-to-one association to DireccionEmpresa
@OneToMany(mappedBy = "empresa", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<DireccionEmpresa> direccionEmpresas;

    //Getters Setters ommited for brevity
    }

但是,这并不是从数据库中删除DireccionEmpresa。。。为什么会发生这种情况?

这很正常。你没有移除empresa,所以没有什么可级联的。您仅从相应的集合中删除了direccionEmpresa。您必须手动删除该对象

这很好地解释了这个案子

JPA2添加了孤儿移除属性,当您从父对象集合中移除子对象时,该属性应负责移除它们。因此,在您的情况下,它将是:

@OneToMany(mappedBy = "empresa", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<DireccionEmpresa> direccionEmpresas;

//Getters Setters ommited for brevity
}
@OneToMany(mappedBy=“empresa”,fetch=FetchType.LAZY,cascade=CascadeType.ALL,orphan=true)
私人名单董事会;
//为简洁起见,建议使用Getters Setters
}

+1。我还要补充一点,对合并的调用是不必要的:实体是附加的。
@OneToMany(mappedBy = "empresa", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<DireccionEmpresa> direccionEmpresas;

//Getters Setters ommited for brevity
}