3

Please check this Entity:

@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
    }

Then in my controller I have something like this:

Empresa empresa=entityManager.findById(1L);
empresa.getDireccionEmpresas.remove(direccionEmpresa);
entityManager.merge(empresa);

However this is not deleting the DireccionEmpresa from the Database... why this could be happening?.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pablo
  • 3,433
  • 7
  • 44
  • 62

1 Answers1

5

It's normal. You didn't remove empresa, so there's nothing to cascade. You only removed direccionEmpresa from the corresponding collection. You have to delete the object manually.

another question on this topic which explains the case pretty well.

JPA 2 added orphanRemoval attribute which should take care of removing child objects when you remove them from parent's collection. So in your case it would be:

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

//Getters Setters ommited for brevity
}
Community
  • 1
  • 1
soulcheck
  • 36,297
  • 6
  • 91
  • 90