1
I have a little problem with JPA which is the following, I have the class ModeloEstoquePecas
, where theoretically I need to delete everything first, which are the children’s records of the class EstoquePecas
, with the code below that I will be putting what happens and I can not solve it theoretically deletes, more at the time that will delete the parent record that is the EstoquePecas
he finds the children records and gives error to delete because he found the children, how to resolve this situation.
public List<ModeloEstoquePecas> destroyByCodigoEstoquePecas (List<ModeloEstoquePecas> listaModeloEstoquePecas) {
EntityManager em = getEntityManager();
if (listaModeloEstoquePecas != null) {
try {
em.getTransaction().begin();
for (ModeloEstoquePecas modeloEstoquePeca : listaModeloEstoquePecas) {
modeloEstoquePeca = em.merge(modeloEstoquePeca);
em.remove(modeloEstoquePeca);
}
em.flush();
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
em.close();
}
}
return listaModeloEstoquePecas;
}
Log:
br.com.escconsultoria.escoficina.controller.exceptions.Illegalorphanexception: This Stockpiles (br.com.escconsultoria.escoficina.model.entity.Estoquepecas[ codeCrackPecas=8 ]) cannot be destroyed Models br.com.escconsultoria.escoficina.model.entity.Modelstoquepecas[ modelEstoquePecasPK=br.com.escconsulting.escoficina.model.entity.Modelstoquepecaspk[ codeStockPecas=8, codeModel=1 ] ] in its modelEstoquePecasList field has a non-nullable stock.
public class ModeloEstoquePecas implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected ModeloEstoquePecasPK modeloEstoquePecasPK;
@Basic(optional = false)
@Column(name = "dataAlteracaoModeloEstoquePecas")
@Temporal(TemporalType.DATE)
private Date dataAlteracaoModeloEstoquePecas;
@JoinColumn(name = "codigoEstoquePecas", referencedColumnName = "codigoEstoquePecas", insertable = false, updatable = false)
@ManyToOne(cascade = CascadeType.ALL, optional = false)
private EstoquePecas estoquePecas;
@JoinColumn(name = "codigoModelo", referencedColumnName = "codigoModelo", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Modelo modelo;
public class ModeloEstoquePecasPK implements Serializable {
@Basic(optional = false)
@Column(name = "codigoEstoquePecas")
private int codigoEstoquePecas;
@Basic(optional = false)
@Column(name = "codigoModelo")
private int codigoModelo;
public class EstoquePecas implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "codigoEstoquePecas")
private Integer codigoEstoquePecas;
@Basic(optional = false)
@Column(name = "nomeEstoquePecas")
private String nomeEstoquePecas;
@Lob
@Column(name = "descricaoEstoquePecas")
private String descricaoEstoquePecas;
@Basic(optional = false)
@Column(name = "dataCadastroEstoquePecas")
@Temporal(TemporalType.DATE)
private Date dataCadastroEstoquePecas;
@Basic(optional = false)
@Column(name = "quantidadeEstoquePecas")
private Integer quantidadeEstoquePecas;
@Basic(optional = false)
@Column(name = "valorCompraEstoquePecas")
private BigDecimal valorCompraEstoquePecas;
@Basic(optional = false)
@Column(name = "valorVendaEstoquePecas")
private BigDecimal valorVendaEstoquePecas;
@Basic(optional = false)
@Column(name = "usuarioEstoquePecas")
private String usuarioEstoquePecas;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "estoquePecas")
private List<ModeloEstoquePecas> modeloEstoquePecasList;
Solved:
private void jButtonEstoquePecasExcluirActionPerformed(java.awt.event.ActionEvent evt) {
EstoquePecas estoquePecas = new EstoquePecas();
if (jTextFieldIncluirCodigoEstoquePecas.getText().isEmpty() == false) {
estoquePecas.setCodigoEstoquePecas(Integer.parseInt(jTextFieldIncluirCodigoEstoquePecas.getText()));
}
EstoquePecasCRUD estoquePecasCRUD = new EstoquePecasCRUD();
estoquePecasCRUD.setAcaoCRUD("EXCLUIR");
estoquePecas = estoquePecasCRUD.modeloPadrao(estoquePecas);
if (estoquePecas != null) {
JOptionPane.showMessageDialog(null, "Estoque De Peças Excluído Com Sucesso.", "Excluir - Estoque De Pecas", 1);
formWindowOpened(null);
} else {
JOptionPane.showMessageDialog(null, "Estoque De Pecas Não Excluído.", "Excluir - Estoque De Pecas", 0);
}
}
public Integer destroy(Integer id) {
try {
EntityManager em = getEntityManager();
EstoquePecasJpaController estoquePecasJpaController = new EstoquePecasJpaController(em.getEntityManagerFactory());
estoquePecasJpaController.destroy(id);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return id;
}
How are the classes
ModeloEstoquePecas
andEstoquePecas
?– Victor Stafusa
@Victorstafusa updated he creates a Modeloestoquepecaspk automatically also what not understood, but he only uses to delete and search the record.
– Ederson Coelho
thanks, but I managed to solve the problem, well it was very simple, I was trying to do a for excluding the Modeloestoquepecas and was giving error in the transaction, was fixed by simply calling the Warehouse Developer by passing the ID where he already excludes the children and I killing myself.
– Ederson Coelho