Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces DataTable + JPA / Hibernate Pagination

It'll be so cool if i can somehow combine both of these framework together in pagination.

Clicking on the next or prev button on the Primefaces Datatable will trigger the query, limiting the query result using JPA.

Also perhaps with some mechanism, the primefaces component can also get the total pages from another JPA select count query ?

Is there any example on how to put these into work ?

Please share your experiences on this.

Thank you !

like image 899
Albert Gan Avatar asked Apr 05 '11 07:04

Albert Gan


2 Answers

You can use a LazyDataModel. In this sample I'm using BackBean and JpaController created by Netbeans with "Create JSF CRUD pages from Entities" (BackBean must be @SessionScoped)

private LazyDataModel<Car> lazyModel;
private int pageSize = 5;

public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}

public int getPageSize() {
    return pageSize;

public void LoadData() {
    lazyModel = new LazyDataModel<Car>() {

        @Override
        public List<Car> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {

            //Sorting and Filtering information are not used for demo purposes just random dummy data is returned  

            List<Car> result = new ArrayList<Car>();

            try {
                result = getJpaController().findCarEntities(pageSize, first);
            } catch (Exception ex) {
                JsfUtil.addErrorMessage(ex, search);
            }

            return result;
        }
    };

    /** 
     * In a real application, this number should be resolved by a projection query 
     */
    lazyModel.setRowCount(getJpaController().getCarCount());
    lazyModel.setPageSize(pageSize);
}

public LazyDataModel<Car> getLazyModel() {
    return lazyModel;
}

I've added

    lazyModel.setPageSize(pageSize);

beacuse the division by 0 know issue http://code.google.com/p/primefaces/issues/detail?id=1544

        <p:dataTable  var="item" value="#{controller.lazyModel}"
                      rows="#{controller.pageSize}" paginator="true"
                      paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                      rowsPerPageTemplate="9,12,15"
                      page=""
                      lazy="true"
                      dynamic="true"
                      id="pnlResult"
                      >  
like image 162
Mirco Attocchi Avatar answered Sep 26 '22 10:09

Mirco Attocchi


This is my code with filter for one column(name column).

  //  @PostConstruct method(injection for ejb's happens after constructor)
   contactsEJB.getEntityManager().getEntityManagerFactory().getCache().evictAll(); //Clear the cache(get fresh data)       

    lazyModel = new LazyDataModel<Contacts>() {

        @Override
        public List<Contacts> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filter) {

            List<Contacts> list = new ArrayList<Contacts>();

            if (!filter.isEmpty()) {
                Iterator it = filter.entrySet().iterator();
                it.hasNext(); //this needs to be a while loop to handle multiple filter columns
                Map.Entry pairs = (Map.Entry) it.next();
                list = contactsEJB.findNameLike(pairs.getValue().toString(), first, pageSize);
                getLazyModel().setRowCount(list.size());
            } else {                 
                list = contactsEJB.findRangeOrder(first, pageSize);                 
                getLazyModel().setRowCount(contactsEJB.count());
            }

            return list;
        }
    };

    getLazyModel().setRowCount(contactsEJB.count());

EJB

public List<Contacts> findRangeOrder(int start, int max) {

    CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    Root<Contacts> root = cq.from(Contacts.class);        
    cq.select(root);
    cq.orderBy(builder.desc(root.get(Contacts_.inserted)));
    Query query = getEntityManager().createQuery(cq);
    query.setMaxResults(max);
    query.setFirstResult(start);
    return query.getResultList();
}

  public List<Contacts> findNameLike(String name, int start, int max) {

    CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    Root<Contacts> root = cq.from(Contacts.class);        
    cq.select(root);        
    Predicate likeName = builder.like(root.get(Contacts_.name), "%"+name+"%");        
    cq.where(likeName);        
    Query query = getEntityManager().createQuery(cq);
    query.setMaxResults(max);
    query.setFirstResult(start);
    return query.getResultList();
}
like image 41
Drew H Avatar answered Sep 24 '22 10:09

Drew H