JPA - Error of record not found soon after insertion of same

5

A question regarding JPA procedures, due to an error that has happened and I can not understand.

I'm taking care of a part of the system that feeds a card order table. It looks up if the user that the request references has the card, and it inserts before inserting in the credits table. However it gives a parent-key error not found, even by inserting it in the card table.

Below what is displayed in the console:

[EL Fine]: sql: 2015-04-24 16:22:50.166--ClientSession(1319007734)--Connection(2018078818)--Thread(Thread[main,5,main])--INSERT INTO CARTAO (CD_CARTAO, |demais campos|) VALUES (?, |demais campos|)
    bind => [10905487, |demais campos|]
[EL Finest]: query: 2015-04-24 16:22:50.17--UnitOfWork(459973678)--Thread(Thread[main,5,main])--Execute query UpdateObjectQuery(br.com.virtus.sgc.model.Pedido@21144823)
[EL Fine]: sql: 2015-04-24 16:22:50.171--ClientSession(1319007734)--Connection(2018078818)--Thread(Thread[main,5,main])--UPDATE PEDIDO SET CD_FASE_ATUAL = ? WHERE (CD_PEDIDO = ?)
    bind => [6, 22557]
[EL Finest]: query: 2015-04-24 16:22:50.174--UnitOfWork(459973678)--Thread(Thread[main,5,main])--Execute query InsertObjectQuery(br.com.virtus.sgc.model.Cardcredito@6e8d1800)
[EL Fine]: sql: 2015-04-24 16:22:50.175--ClientSession(1319007734)--Connection(2018078818)--Thread(Thread[main,5,main])--INSERT INTO CARDCREDITO (ID_CARDCREDITO,  VL_CREDITO, CD_CARTAO, CD_PEDIDO) VALUES (?, ?, ?, ?)
    bind => [1721388, 356, 10905487, 22557]
[EL Fine]: sql: 2015-04-24 16:22:50.182--ClientSession(1319007734)--Thread(Thread[main,5,main])--SELECT 1 FROM DUAL
[EL Warning]: 2015-04-24 16:22:50.188--UnitOfWork(459973678)--Thread(Thread[main,5,main])--Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-02291: restrição de integridade (SGCMASTER.RI_CARDCREDITO) violada - chave-pai não localizada

The console displays the same card number: 10905487 . This ID is generated by an Oracle Sequence.

Of course, the code:

public void processar(){
        BigDecimal icCardcredito;
        try {
            em.getTransaction().begin();
            List<Pedido> lstAProcessar  = this.listagemPedidosAProcessar();
            TipoFasePedido proc         = new TipoFasePedidoImpl().pesquisarPorId(Long.valueOf(qryParam.pesquisarPorNmChave("CD_FASE_PEDIDO_PROCESSADO").getNmValor()));
            HistoricoPedido histPed     = new HistoricoPedido();            
            for(Pedido ped:lstAProcessar){
                ped.setCardcreditos(new ArrayList<Cardcredito>());
                icCardcredito = BigDecimal.ONE;
                List<PedidoDetalhe> infoPedido = this.listaValores(ped);
                for (PedidoDetalhe info: infoPedido){
                    Produto prod = ped.getProduto();
                    Cliente clie = ped.getCliente();
                    String  cpf  = info.getCdCpf();
                    Cartao  card = new Cartao(); 
                    card         =  this.pegaCartao(cpf, clie, prod);//Aqui ele busca o cartão
                    boolean tem  = (card instanceof Cartao);
                    if (!(tem)){
                        card = novoCartao(info);
                        em.persist(card);
                    }
                    Cardcredito itemCred = new Cardcredito();
                    itemCred.setCartao(card);
                    itemCred.setVlCredito(info.getVlCredito());
                    ped.addCardcredito(itemCred);
                    em.merge(ped);
                    icCardcredito.add(BigDecimal.ONE);
                }
                histPed = new HistoricoPedido();

                histPed.setTipoFasePedido(proc);
                histPed.setDtFasePedido(new Date());
                histPed.setNmOperador(qryUtils.nmUsuarioSistema());

                ped.setTipoFasePedido(proc);
                ped.addHistoricoPedido(histPed);

                em.merge(ped);                      
            }       
        em.getTransaction().commit();
        } catch (Exception ex) {
            em.getTransaction().rollback();
            System.out.println("---ERRO!----" + ex.getMessage());
        }

    }

My question is:

What is the explanation for this? Does it have anything to do with cascadeType between tables?

I tested with ALL and PERSIST and both gave this error.

Thanks!

[ Updating ]

I was checking the base and realized that the triggers were active (to deal with the sequences , I am using @GeneratedValue and @SequenceGenerator in the template.) Inactivated and apparently this .

But still something curious happened.

The card table has a related history table. It was fed during the process, and after it had disabled triggers , it was giving error exactly in it (of parent key not found). I took the information TIMESTAMP of the date and put DATE and went through this step normally.

What relationship exists between date fields and a PK?

    
asked by anonymous 24.04.2015 / 22:04

1 answer

0

I ended up checking the database and noticed that the triggers were active. Just as the @GeneratedValue and @SequenceGenerator were being used in the class.

So, the id was updated within the class, but was changed at the base by the trigger, which called the sequence again.

Inactivated the trigger, so the sequence is updated only one way, and only once.

    
20.08.2015 / 17:05