My List is not updated correctly with bank data

3

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

HQL:

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

ListList method that stays in 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 loads 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 stays on the same screen, one is for data received and this is for the 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();
        }

    }
    
asked by anonymous 26.06.2015 / 13:29

1 answer

2

I believe you are not forcing% reloading after saving in the bank. In your Submit method after you have done dataTable you need to redo the loading of your Save .

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();
    }

}


Home If you are using @ViewScoped and can not change to @ConversationScoped , you will need to put 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 your MB of list

@ManagedBean
@ViewScoped
public class SeuOutroBean implements Serializable {

    private ControleAuditoriaLiberacaoBean controleAuditoriaLiberacao;

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

}

And on your Dialog call

controleAuditoriaLiberacao.carregarPesquisa();
    
26.06.2015 / 13:40