List 5 objects from a List

Asked

Viewed 40 times

1

I’m putting together a panel that should show 5 products, for example, on a page index, but I can only load all the objects that are saved in the comic book, and I can’t think of a way to do that, any alternative would be, by DAO, charging in a class or even at the front. Would they have any solution?

Front:

    <c:forEach items="${dao.listarPlat('PS4')}" var="p">
                                    <div class="col-md-3 produto well i2">
                                            <img class="img" src="img/${p.foto1}" title="" alt="">
                                            ${p.titulo}<br>${p.plataforma}
                                                R$
                                                <f:formatNumber minFractionDigits="2" currencySymbol="R$">
                                                    ${p.preco}
                                                </f:formatNumber>
                                        <button class="btn btn-success">Comprar</button>
                                    </div>    
                                </c:forEach>

DAO:

    public List<Produto> findProdutosPlat(String plataforma) {
    try {
        Query query = em.createQuery("select p from Produto as p where p.plataforma like :plataforma");
        query.setParameter("plataforma", plataforma + "%");
        List<Produto> produtos = query.getResultList();
        return produtos;
    } finally {
        em.close();
    }
}

Ctrlproduto:

    public List<Produto> listarPlat(String dados) throws Exception {
    ProdutoDAO dao = new ProdutoDAO();
    return dao.findProdutosPlat(dados);
}

The solution was to add to the DAO the .setMaxResults(5), being like this:

    public List<Produto> findProdutosMenu(String plataforma) {
    try {
        Query query = em.createQuery("select p from Produto as p where p.plataforma like :plataforma");
        query.setParameter("plataforma", plataforma + "%");
        query.setMaxResults(5);
        List<Produto> produtos = query.getResultList();
        return produtos;
    } finally {
        em.close();
    }
}
  • What is the DBMS? Mysql? Sqlserver?

  • I am using mysql, run by Phpmyadmin

  • 1

    http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html, using the setMaxResult in his query, works?

  • 1

    @Jeffersonquesado said and done. Thank you very much!

  • 1

    Okay, now post the answer explaining how you managed to solve the problem =] so someone who has had your same problem will be able to consult in the answer the solution

No answers

Browser other questions tagged

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