My List is not updated correctly with database data

Asked

Viewed 152 times

3

I have a dataTable and a button that selects the object in each line of the dataTable. When I click on that one button Dialog is opened with some data and a Send button, when I click this send the selected row is edited in the database. So far everything works, but my list that fills the dataTable is not loaded with the right data, in the database is right but when I do the query in the system the list always returns what it already had before (does not update). The strange thing is that only after about 20 seconds and a F5 is that this list is updated correctly. Can anyone help?

HQL:

@NamedQuery(name = "SolicitacaoBD.listarPorUsuario", query = "SELECT solicitacoesBD FROM SolicitacoesBD solicitacoesBD WHERE solicitacoesBD.enviadoPor = :enviadoPor AND solicitacoesBD.status = 'Aguardando'")

List methodUsuario located in the DAO:

public List<SolicitacoesBD> listarPorUsuario(String enviadoPor) {
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        List<SolicitacoesBD> lista = null;
        try {
            Query consulta = sessao
                    .getNamedQuery("SolicitacaoBD.listarPorUsuario");
            consulta.setString("enviadoPor", enviadoPor);
            lista = consulta.list();

        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            sessao.close();
        }
        return lista;
    }

And in my Bean I have the method that carries the dataTable.

@PostConstruct
    public void carregarPesquisa() {
    list = new ArrayList<SolicitacoesBD>();
        System.err.println("Pesquisa Auditor");
        try {

            SolicitacoesDAO solicitacaoDAO = new SolicitacoesDAO();
            list = solicitacaoDAO.listarPorUsuario("Liberacao");

            //Essa lista nunca é atualizada de imediato mesmo o banco estando certo
            System.out.println("Lista Auditor:" +list);
            carregarEnviados();
        } catch (RuntimeException ex) {
            FacesUtil.adicionarMsgErro("Erro ao tentar listar as Auditorias");
        }
    }

I have another method that loads another datatable that is on the same screen, one is for data received and this is for data sent:

// mostra na tabela todos os dados enviados
    public void carregarEnviados() {
        try {
            SolicitacoesDAO solicitacaoDAO = new SolicitacoesDAO();
            listEnviados = solicitacaoDAO.listarEnviados("Auditor");

        } catch (RuntimeException ex) {
            FacesUtil.adicionarMsgErro("Erro ao tentar listar as Auditorias");
        }
    }

Method that edits the selected line, this method is in the Bean that controls the Dialog.

public void editar() {
        try {
            //Anexo de Arquivo
            solicitacoesBD.setCaminhoArquivo(destination + nomeArquivo);
            copyFile(nomeArquivo, input);
            solicitacoesBD.setNomeArquivo(nomeArquivo);
            System.out.println("Arquivo nomeeeeeee: "
                    + solicitacoesBD.getNomeArquivo());
            //Anexo de Arquivo

            SolicitacoesDAO solicitacaoDao = new SolicitacoesDAO();
            solicitacaoDao.editar(solicitacoesBD);

            FacesUtil.adicionarMsgInfo("Solicitação Enviada com Sucesso");

        } catch (RuntimeException e) {
            FacesUtil.adicionarMsgErro("Erro ao Enviar Solicitação!");
            e.printStackTrace();
        }

    }
  • which method saves data from the database dialog?

  • I’ll edit the question and put

  • ready, I put a Sysout in my DAO to display the list data too and even then the list is wrong, I look at the database and everything is ok but the query returns values that should not appear.

1 answer

2


I believe you are not forcing the recharging of dataTable after saving to the bank. In your Send after doing the Save you need to re-load your list.

public void editar() {
    try {
        //Anexo de Arquivo
        solicitacoesBD.setCaminhoArquivo(destination + nomeArquivo);
        copyFile(nomeArquivo, input);
        solicitacoesBD.setNomeArquivo(nomeArquivo);
        System.out.println("Arquivo nomeeeeeee: "
                + solicitacoesBD.getNomeArquivo());
        //Anexo de Arquivo

        SolicitacoesDAO solicitacaoDao = new SolicitacoesDAO();
        solicitacaoDao.editar(solicitacoesBD);

        // Recupera seu outro bean e força o carregamento
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        SeuBeanDaDataTable seuBeanDaDataTable = (SeuBeanDaDataTable) elContext.getELResolver().getValue(elContext, null, "SeuBeanDaDataTable");
        seuBeanDaDataTable.carregarPesquisa();

        FacesUtil.adicionarMsgInfo("Solicitação Enviada com Sucesso");

    } catch (RuntimeException e) {
        FacesUtil.adicionarMsgErro("Erro ao Enviar Solicitação!");
        e.printStackTrace();
    }

}




If using @Viewscoped and cannot be changed to @Conversationscoped, you’ll need to put in Request Scope your MB.

@ManagedBean
@ViewScoped
public class ControleAuditoriaLiberacaoBean implements Serializable {

    public String submit() {
        FacesContext.getCurrentInstance().getExternalContext()
            .getRequestMap().put("controleAuditoriaLiberacao", this);
        return "valorAction";
    }    
}

And in his MB of Dialog

@ManagedBean
@ViewScoped
public class SeuOutroBean implements Serializable {

    private ControleAuditoriaLiberacaoBean controleAuditoriaLiberacao;

    @PostConstruct
    public void init() {
        controleAuditoriaLiberacao = (ControleAuditoriaLiberacaoBean) FacesContext.getCurrentInstance().getExternalContext()
            .getRequestMap().get("controleAuditoriaLiberacao");
    }

}

And in his editar() calling

controleAuditoriaLiberacao.carregarPesquisa();

Browser other questions tagged

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