Autoincrement error in jpa

1

This message I get when you perform the insert in the database and it does not give any errors, it only gives this message of warning .

  

Warning: The class RepeatableWriteUnitOfWork is already flushing. The query will be executed without further changes being written to the database. If the query is conditional upon changed data the changes may not be reflected in the results. Users should issue a flush () call upon completion of the dependent changes and prior to this flush () to ensure correct results.

    NclDocumento documento = null;
    try {
        NclTipoDocumento tipo = findTipoDocumentoById(tipoD.getValue());
        if (numero != null && numero != 0 && anioSelected != null) {
            documento = findAcuerdoByNumeroAndAnioAndTipoDocumento(numero, anioSelected, tipo);

            if (documento != null) {
                return new Response<>(documento, ResponseStatus.OK_INSERT);
            }

        }

        if (documento == null) {
            documento = new NclDocumento();
            documento.setNumero(122);
            documento.setAnio(2014);
            documento.setFechaEmision(new Date());
            documento.setIdTipoDocumento(em.find(NclTipoDocumento.class,1));
           if (firma != null && firma.getEmpleado() != null) {
               documento.setIdFirma(firma.getEmpleado());
            }
            documento.setFechaCreacion(new Date());
            documento.setUsuarioCreacion(usuarioLogueado);
            documento.setActivo(true);
        }

        em.persist(documento);
        em.flush();
        return new Response<>(documento, ResponseStatus.OK_INSERT);
    } catch (ConstraintViolationException ex) {
        String validationError = getConstraintViolationExceptionAsString(ex);
        log.error(validationError);
        context.setRollbackOnly();
        return new Response<>(ResponseStatus.ERROR_PARAMS, "Sucedio un error con la información: " + validationError);
    } catch (Exception ex) {
        processException(ex);
        context.setRollbackOnly();
        return new Response<>(ResponseStatus.ERROR, "Sucedio un error: " + ex.getMessage());
    }
    
asked by Raul Cacacho 11.12.2017 в 22:45
source

1 answer

2

What you are doing em.flush(); is doing what would come to be an "immediate execution" of em.persist(documento); . The correct use of the flush() is when you want to perform the execution of the sql instructions that are waiting for the commit to execute some others that may depend on them, such as bringing the autoincrementable id of em.persist(documento); to perform other operations with the entityManager.

The warning tells you that you are forcing a flush() when one is already in progress (as is normal when committing). Look at it as if you were doing a double commit at the end and the implementer is telling you: "You are sending a commit that is not having an effect because it is next to another: Are you sure you should not go before?"

It is not an error, but a warning that something is happening to you. Maybe you have autocommit and the flush should not go.

    
answered by 11.12.2017 / 23:36
source