Object is not updated

2

In my stream it looks like this:

  • I already have an object stored in the database.
  • When loading my page, these objects are fetched in the database, treated to be displayed.
  • Objects arrive in a List ( List ), that is iterated by for to receive the appropriate treatments.
  • While an object is handled, I make a query of webservice receiving another object. I make a comparison between objects and if there is a difference, the object that came from the database needs to be updated.

Basically this is, the problem occurs is that I update my object and have it updated in the database but does not update. Can someone give me a help?

The method you are looking for in the database:

public void pesquisar() {
        List<Objeto> objetos = new ArrayList<>();
        objetos = service.findAll();
        this.encomendas = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(objetos)) {
            try {
                for (Objeto objeto : objetos) {
                    houveAlteracao(objeto);
                    ajustarInformacoes(objeto);
                }
                ordenarLista();
                horaAtualizacao = new Date();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Now the houveAlteracao() method:

private void houveAlteracao(Objeto objeto) {
        try {
            final Objeto obj = popularObjeto(objeto.getCodigo());
            if (obj.getEventos().size() > objeto.getEventos().size()) {
                objeto.setEventos(obj.getEventos());
                service.update(objeto);
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

In this method if getEventos.size() that belongs to the searched object in webservice is higher, the database object must receive these values and then updated, the problem is that the command is applied but does not update. And finally the update command:

public void update(Objeto objeto) {
        this.em.merge(objeto);
    }

Does anyone help please?

    
asked by anonymous 31.03.2017 / 23:58

1 answer

0

Thanks to everyone who tried to help, but by reading this Article clear to me, so it worked:

private void houveAlteracao(Long id) {
        try {
            Objeto objeto = service.find(id);
            final Objeto obj = popularObjeto(objeto.getCodigo());
            if (obj.getEventos().size() > objeto.getEventos().size()) {
                Evento evento = obj.getEventos().get(0);
                objeto.getEventos().add(evento);
                evento.setHorario(JsfUtils.stringParaTimestamp(evento.getDataOcorrencia().concat(" ").concat(evento.getHoraOcorrencia())));
                evento.setObjeto(objeto);
                service.update(objeto);
                pesquisar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
01.04.2017 / 17:45