JPA does not update children record [closed]

1

I have a problem with JPA which is as follows, I have the class ModeloEstoquePecas , where theoretically I need to delete everything first, which are the children records of class EstoquePecas , with the code below that I'll be putting what happens and I can not solve it theoretically deletes, but at the time it will delete the parent record which is EstoquePecas it finds the children records and gives error to delete because it found the children, how to solve this situation. / p>

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 StockPecas   (br.com.escconsultoria.escoficina.model.entity.EstoquePecas [   codigoEstoquePecas = 8]) can not be destroyed since the   ModelEstoquePecas   br.com.escconsultoria.escoficina.model.entity.ModeloEstoquePecas [   modelEstoquePecasPK = br.com.escconsultoria.escoficina.model.entity.ModeloEstoquePecasPK [   codeEstoquePecas = 8, codeModelo = 1]] in its modelEstoquePecasList   field has a non-nullable stockPecas field.

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;

Resolved:

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;
}
    
asked by anonymous 24.08.2017 / 04:33

0 answers