Primefaces DataTable + JPA / Hibernate Pagination

拈花ヽ惹草 提交于 2019-12-17 17:54:08

问题


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 !


回答1:


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"
                      >  



回答2:


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();
}


来源:https://stackoverflow.com/questions/5548587/primefaces-datatable-jpa-hibernate-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!