Object is not updated

Asked

Viewed 300 times

3

In my flow it’s like this:

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

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

The method you search in the bank:

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 method houveAlteracao() :

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() which belongs to the object searched in webservice is larger, the database object must receive these values and then updated, the problem is that the command is applied but not updated. And finally the update command:

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

Somebody please help me ?

  • You can add fields to some table if necessary?

  • If your object is managed by Hibernate you should use the method update() instead of merge(). If it is not managed by Hibernate you should use the merge() and then call the method persist(). I think this. Always confusing me with these methods hehe

  • @Victorstafusa did not understand your question.

  • The problem is actually another. I’m going to create a new question.

1 answer

1

Thanks to everyone who tried to help but reading this Article of course for me, then 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();
        }

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.