HTTP Status 500 - Lazy loading is not implemented.
type Exception report
message Lazy loading is not implemented.
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Lazy loading is not implemented.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
java.lang.UnsupportedOperationException: Lazy loading is not implemented.
org.primefaces.model.LazyDataModel.load(LazyDataModel.java:112)
org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:1024)
org.primefaces.component.datatable.DataTableRenderer.preRender(DataTableRenderer.java:97)
org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M15 logs.
Trecho de livro.xhtml:
<h:form id="formTabelaLivros">
<p:dataTable value="#{livroBean.livroDataModel}" var="livro" id="tabelaLivros" paginator="true" rows="5" lazy="true" sortMode="multiple">
<f:facet name="header">Livros cadastrados</f:facet>
<p:column headerText="Título" sortBy="#{livro.titulo}" filterBy="#{livro.titulo}" filterMatchMode="startsWith">
<h:outputText value="#{livro.titulo}" />
</p:column>
<p:column headerText="ISBN" sortBy="#{livro.isbn}" filterBy="#{livro.isbn}" filterMatchMode="contains">
<h:outputText value="#{livro.isbn}" />
</p:column>
<p:column headerText="Preço" sortBy="#{livro.preco}" filterBy="#{livro.preco}" filterFunction="#{livroBean.precoEhMenor}">
<h:outputText value="#{livro.preco}">
<f:convertNumber type="currency" pattern="R$ #0.00"
currencySymbol="R$" locale="pt_BR" />
</h:outputText>
</p:column>
<p:column headerText="Data" sortBy="#{livro.dataLancamento.time}" filterBy="#{livro.dataLancamento.time}" filterMatchMode="contains">
<h:outputText value="#{livro.dataLancamento.time}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:outputText>
</p:column>
<p:column headerText="Alterar">
<h:commandLink value="Alterar" action="#{livroBean.carregar(livro)}"/>
</p:column>
<p:column headerText="Remover">
<h:commandLink value="Remover" action="#{livroBean.remover(livro)}"/>
</p:column>
</p:dataTable>
</h:form>
Classe livroDataModel:
public class LivroDataModel extends LazyDataModel<Livro>{
private static final long serialVersionUID = 1L;
private DAO<Livro> dao = new DAO<Livro>(Livro.class);
public LivroDataModel() {
super.setRowCount(dao.quantidadeDeElementos());
}
public List<Livro> load(int inicio, int quantidade, String campoOrdenacao, SortOrder sentidoOrdenacao, Map<String, Object> filtros) {
String titulo = (String) filtros.get("titulo");
return dao.listaTodosPaginada(inicio, quantidade, "titulo", titulo);
}
}
Métodos acrescentados ao DAO:
public List<T> listaTodosPaginada(int firstResult, int maxResults, String coluna, String valor) {
EntityManager em = new JPAUtil().getEntityManager();
CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
Root<T> root = query.from(classe);
if(valor != null)
query = query.where(em.getCriteriaBuilder().like(root.<String>get(coluna), valor + "%"));
List<T> lista = em.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
em.close();
return lista;
}
public int quantidadeDeElementos() {
EntityManager em = new JPAUtil().getEntityManager();
long result = (Long) em.createQuery("select count(n) from " + classe.getSimpleName() + " n")
.getSingleResult();
em.close();
return (int) result;
}