DataTable data is not displayed, even with loading of ArrayList being executed correctly

1

I'm loading a JSF dataTable with an ArrayList, but the result is not displayed at all, here's my code:
index.xhtml               

<h:body>
    <h1>Novo Jogo</h1>
    <j:form>
        <fieldset>
            <legend>Dados do Jogo</legend>
            <j:panelGrid columns="2">
                <j:outputLabel value="Titulo:" for="titulo" />
                <j:inputText id="titulo" value="#{meusJogosBean.mj.titulo}" />
                <j:outputLabel value="Desenvolvedora:" for="desenvolvedora" />
                <j:inputText id="desenvolvedora"
                    value="#{meusJogosBean.mj.desenvolvedora}" />
                <j:outputLabel value="Ano Lancamento:" for="ano_lancamento" />
                <j:inputText id="ano_lancamento"
                    value="#{meusJogosBean.mj.ano_lancamento}" />
                <j:outputLabel value="plataforma:" for="plataforma" />
                <j:inputText id="plataforma" value="#{meusJogosBean.mj.plataforma}" />
                <j:commandButton value="Gravar" action="#{meusJogosBean.gravar}">
                </j:commandButton>
            </j:panelGrid>
        </fieldset>
    </j:form>
<ui:define name="conteudo">
    <h:form id="formTabelaJogos">
        <p:dataTable value="#{meusJogosBean.jogos}" var="j" id="tabelaJogos" lazy="true">
                <p:column headerText="Título" >
                    <h:outputText value="#{j.titulo}"/>
                    <h:outputText value="text" />
                </p:column>
    </p:dataTable>
    </h:form>
</ui:define>

</h:body>
</html>


MyGames.Bean.java

package model;


import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import model.MeusJogos;
import model.DAO;

@ManagedBean
@ViewScoped
public class MeusJogosBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private MeusJogos mj = new MeusJogos();

    public List<MeusJogos> getJogos() {
        return new DAO<MeusJogos>(MeusJogos.class).listaTodos();
    }

    public MeusJogos getMj() {
        return mj;
    }

    public void setMj(MeusJogos mj) {
        this.mj = mj;
    }

    public void gravar() {
        System.out.println("Gravando jogo: " + this.mj.getTitulo());
        new DAO<MeusJogos>(MeusJogos.class).adiciona(mj);

    }


}

MyGames.Java

package model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class MeusJogos {
    @Id @GeneratedValue
    private Integer id;
    private String titulo;
    private String desenvolvedora;
    private String ano_lancamento;
    private String plataforma;

    public MeusJogos() {

    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getDesenvolvedora() {
        return desenvolvedora;
    }
    public void setDesenvolvedora(String desenvolvedora) {
        this.desenvolvedora = desenvolvedora;
    }
    public String getAno_lancamento() {
        return ano_lancamento;
    }
    public void setAno_lancamento(String ano_lancamento) {
        this.ano_lancamento = ano_lancamento;
    }
    public String getPlataforma() {
        return plataforma;
    }
    public void setPlataforma(String plataforma) {
        this.plataforma = plataforma;
    }

}

DAO.java

package model;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;

public class DAO<T> {

    private final Class<T> classe;

    public DAO(Class<T> classe) {
        this.classe = classe;
    }

    public void adiciona(T t) {

        // consegue a entity manager
        EntityManager em = new JPAUtil().getEntityManager();

        // abre transacao
        em.getTransaction().begin();

        // persiste o objeto
        em.persist(t);

        // commita a transacao
        em.getTransaction().commit();

        // fecha a entity manager
        em.close();
    }

    public void remove(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.remove(em.merge(t));

        em.getTransaction().commit();
        em.close();
    }

    public void atualiza(T t) {
        EntityManager em = new JPAUtil().getEntityManager();
        em.getTransaction().begin();

        em.merge(t);

        em.getTransaction().commit();
        em.close();
    }

    public List<T> listaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).getResultList();

        System.out.println(lista.size());
        em.close();
        return lista;

    }

    public T buscaPorId(Integer id) {
        EntityManager em = new JPAUtil().getEntityManager();
        T instancia = em.find(classe, id);
        em.close();
        return instancia;
    }

    public int contaTodos() {
        EntityManager em = new JPAUtil().getEntityManager();
        long result = (Long) em.createQuery("select count(n) from jogo n")
                .getSingleResult();
        em.close();

        return (int) result;
    }

    public List<T> listaTodosPaginada(int firstResult, int maxResults) {
        EntityManager em = new JPAUtil().getEntityManager();
        CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
        query.select(query.from(classe));

        List<T> lista = em.createQuery(query).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();

        em.close();
        return lista;
    }

}
    
asked by anonymous 30.04.2017 / 18:44

2 answers

1

If you are using the lazy="true" attribute, you must treat data modeling with the LazyDataModel class.

The lazy attribute causes the pagination to be "real", that is, it causes each page change in the dataTable to make a new request to the back end, reaching the database with at least two information: the index of the line from where it should return the data and the amount of data that should be returned. For example: if you have a query that results in 100 records and your pagination in the dataTable is 10 items per page, when the user clicks on the second page the information 11 and 10 will be sent, 11 being index from where the data should return and the 10 the amount of data that should be returned.

Usually the lazy is used when working with a considerable database. For a few records sometimes (I repeat, sometimes ) it is best to leave the "false" paging, where at the time of loading the dataTable, that it is already loaded completely and pagination is only in the view.

If you want to use lazy , you need to create a class that inherits from LazyDataModel<T> . For example:

import org.primefaces.model.LazyDataModel;

public class LazyJogosDataModel extends LazyDataModel<MeusJogos> { }

In this class you need to override (at least) the load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) method. (If I am not mistaken there is an overload of this method that receives only the first two parameters)

At first it's a little complicated / boring to understand how it works. But once you get the hang of it, you'll be fine.

I strongly advise you to take a look at in this example on the PrimeFaces website . It is very good and quite complete.

    
03.05.2017 / 13:27
1

Do not use List when using lazy = true. puts it like this:

LazyDataModel<MeusJogos>  = new LazyDataModel(){

  public load (...){
    DAO<MeusJogos>(MeusJogos.class).listaTodos();

  }
}

will solve your problem

    
02.05.2017 / 19:41