Table does not update correctly after editing

Asked

Viewed 205 times

0

I have a dataTable and in the last column I have an edit button that opens a dialog with a field to be filled. When I click edit the table is updated with the edited data, but if I give an F5 the old data comes back to dataTable.

Edit method in bean:

public void pausarTarefa(){
        System.err.println("Pausar");
        try {
            tarefa.setStatus("Pausado");
            tarefa.setDataFim(new Date());

            TarefaDAO tarefaDAO = new TarefaDAO();
            tarefaDAO.editar(tarefa);   
            FacesUtil.adicionarMsgInfo("Tarefa Pausada com Sucesso");
            tarefa = new Tarefa();
        } catch (RuntimeException e) {
            FacesUtil.adicionarMsgErro("Erro ao Pausar Tarefa");
            e.printStackTrace();
        }
    }

Method that fills the list used in the datatable:

@PostConstruct
public void listar() {
    listaTarefa = new ArrayList<>();
    System.err.println("Metodo Listar");
    try {
        TarefaDAO tarefaDAO = new TarefaDAO();
        listaTarefa = tarefaDAO.listarPorUsuario(usuarioBean
                .getUsuarioLogado());

    } catch (RuntimeException e) {
        FacesUtil.adicionarMsgErro("Erro ao listar tarefas: "+e.getMessage());
    }
}

The scope of the bean is ViewScoped. In the database the data is correct after editing, it is only the datatable that brings the old data. Someone can help?


Datatable:

p:dataTable value="#{tarefaBean.listaTarefa}" id="tarefaTable"
                var="tarefa" style="margin-top: 20px"
                emptyMessage="Nenhuma Tarefa Encontrada. " rows="10"
                paginator="true" paginatorAlwaysVisible="false"
                paginatorPosition="botton">
  • Post the code of your datatable (only her statement).

  • I edited the question with the code

2 answers

1

I will explain what is happening. First your dataTable is calling the listaTarefa but when you call the method pausarTarefa() you’re not updating your listaTarefa and its dataTable.

What you can do to solve your problem: within the method pausarTarefa() call the method listar(), so your list will be updated. Well now last you need to update your dataTable, use update button(update="tarefaTable") or a <p:ajax> whichever.

Edit:

change your method, remove the @PostConstruct:

 public List<Tarefa> listar() {
        List<Tarefa> lista = new ArrayList<>();
        System.err.println("Metodo Listar");
        try {
            TarefaDAO tarefaDAO = new TarefaDAO();
            lista = tarefaDAO.listarPorUsuario(usuarioBean.getUsuarioLogado());
        } catch (RuntimeException e) {
            FacesUtil.adicionarMsgErro("Erro ao listar tarefas: " + e.getMessage());
        }
        return lista;
    }

and alters its dataTable:

p:dataTable value="#{tarefaBean.listar}" id="tarefaTable"
                var="tarefa" style="margin-top: 20px"
                emptyMessage="Nenhuma Tarefa Encontrada. " rows="10"
                paginator="true" paginatorAlwaysVisible="false"
                paginatorPosition="botton">
  • I was calling the list method in <f:Event> at the beginning of the page. I did what Voce said the table updates, but when I give an F5 back everything as it was. I’ll make a video to show you so you can understand better

  • Take a look at the video to better understand what happens: https://youtu.be/yeA5yYqOszw

  • edited my answer.

  • It works, but it keeps going back to the previous state after F5. You want me to share the project with Voce on Github?

  • Now I’m in the service, are you sure your task has been updated in the database? and try to also clear your browser cache

  • I do. I log into the bank and it’s up to date, I’ve tried to clear the cache and even log in anonymous to the browser and nothing..

  • Can it be my Namedquery? @NamedQuery(name = "Tarefa.listarPorCodigo", query = "SELECT tarefa FROM Tarefa tarefa WHERE tarefa.usuario = :usuario"),

Show 2 more comments

0

Problem solved. Talking to a friend I found that appointments also have to be committed. So my DAO consultation was this way:

@SuppressWarnings("unchecked")
      public List<Tarefa> listarPorUsuario(Usuario usuario) {
       Session sessao = HibernateUtil.getSessionFactory().openSession();
       Transaction transacao = sessao.beginTransaction();

       List<Tarefa> lista = new ArrayList<>();
       try {
        Query consulta = sessao.getNamedQuery("Tarefa.listarPorCodigo");
        consulta.setParameter("usuario", usuario);
        lista = consulta.list();
        System.err.println("LISTA no DAO: "+lista);
        transacao.commit();
       } catch (RuntimeException ex) {
        throw ex;
       } finally {
        sessao.close();
       }
       return lista;
      }

Browser other questions tagged

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