Load a large volume of data into a datatable

Asked

Viewed 426 times

2

I’m having the following problem, I can load the data into my dataTable but it gets very heavy, until the IDE one hangs. How could I fix this problem? I heard about Lazy but I don’t understand how I can implement.

List method:

public void listar() {
        try {
            if (listaBeneficiario == null) {
                listaBeneficiario = new ArrayList<>();
            }

            System.err.println("Metodo Listar" + listaBeneficiario);

            BeneficiarioDAO beneficiarioDAO = new BeneficiarioDAO();
            listaBeneficiario = beneficiarioDAO.buscar();

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

    }

2 answers

2

Following is an example of the code to implement Lazydatamodel:

Mymb.java

List<NameClass> listResult = new LazyDataModel<NameClass>() {

  private static final long serialVersionUID = 1L;

  @Override
  public List<NameClass> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {

    List<NameClass> result = dao.findReg(....., pageSize, page);
    setRowCount(numberOfResult);
    return result;
  }
};

getListResult() {..}
setListResult(List<NameClass> l ) {...}

myPage.xhtml

<p:dataTable lazy="true" 
 value="#{myMB.listResult}" 
 var="model" 
 paginator="true"
 rows="#{myMB.pageSize}" 
 paginatorPosition="bottom" 
 paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" >

Taking advantage, below the solution in case the pager does not restart:

1 - On the datatable screen, put the tag below, as the most appropriate action in your case. In mine, when closing the modal I call a Istener:

<p:ajax event="close"   listener="#{myMB.onCloseModal}"/> 

2- On the button that closes the modal, put "PF('dialogId'). clearFilters();" :

 <h:commandLink onclick="PF('dialogWidget').hide(); PF('dialogId').clearFilters();" 

3 - No Mymb:

public void onCloseModal(CloseEvent event){
        pageItem = 1;

        //pra funcionar o reset da paginaçao. E reduzir linhas de código. O ideal é que o nome da datatable de item seja o mesmo nome do "dialog+DtTable"
        String nomeDataTable = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("javax.faces.source");

        if( StringUtils.isNotEmpty(nomeDataTable) ){
            DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(nomeDataTable+"DtTable");
            dataTable.setFirst(0);
            dataTable.reset();
            dataTable.setFilterBy(null);
            dataTable.setSortBy(null);

            RequestContext requestContext = RequestContext.getCurrentInstance();
            requestContext.update(nomeDataTable);
        }
    }

0


You can use paging in your datatable. Example:

<rich:dataTable value="#{seuBB.lista}" var="obj"
    id="tabela" noDataLabel="Tabela vazia"
    rows="10">

    <rich:column>
        <f:facet name="header">
            <h:outputText value="ID"
        </f:facet>
        <h:outputText value="#{obj.id}" />
    </rich:column>

    ... outras colunas ...

    <f:facet name="footer">
        <rich:dataScroller renderIfSinglePage="false" />
    </f:facet>
</rich:dataTable>

So you define how many rows per page your table will have rows="10" and there in the footer, that dataScroller promotes pagination if there is more than one page.

EDIT: There are two types of map uploads @OneToMany . @OneToMany(fetch=FetchType.LAZY) and @OneToMany(fetch=FetchType.EAGER).
Read a little about it here

Briefly,

The idea of Lazy is not to bring unnecessary objects


While EAGER always brings all objects related to that class, LAZY only brings them when you explicitly load them, through a JOIN FETCH in the search (when using HQL) or createAlias, using Criteria.

EDIT: The Lazy you’re referencing is the datatable Azy, right? From what I’ve seen, this implementation is possible only with the datatable of the first faces... Here at showcase has an example of how to do this

  • I already have paging, the problem is that when the list method is called it loads the 14 thousand records from the bank.

  • see if I contributed anything else after EDIT

  • It got me a little confused as I had seen this about Lazy: http://blog.algaworks.com/paginacao-de-datatable-do-primefaces-com-lazy-loading/

  • From what I read it is ideal to work with queries that return thousands of records and to avoid memory bursting

  • ah yes... the Lazy that is talking there is of the datatable... the Lazy that I spoke in the answer is of the list objects, with its bank relationships with other entities...

  • see the response issue

  • I’ll take a look

Show 2 more comments

Browser other questions tagged

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