Table does not update correctly after editing

0

I have a dataTable and in the last column I have an edit button that opens a dialog with a field to fill. When I click on edit the table is updated with the edited data, however if I give an F5 the old data will return to appear in 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's just the dataTable that brings the old data. Can anyone 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">
    
asked by anonymous 14.07.2015 / 18:38

2 answers

1

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

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

Edit:

change your method, remove @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 change your 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">
    
14.07.2015 / 19:37
0

Problem solved. Talking with a friend I discovered that the consultations also have to be committees. So my DAO query looked like this:

@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;
      }
    
14.07.2015 / 21:17