Primefaces 数据表 + JPA / Hibernate 分页

发布于 2024-10-31 01:45:51 字数 214 浏览 1 评论 0原文

如果我能以某种方式将这两个框架在分页中组合在一起,那就太酷了。

单击 Primefaces 数据表上的下一个或上一个按钮将触发查询,从而限制使用 JPA 的查询结果。

也许通过某种机制,primefaces 组件还可以从另一个 JPA 选择计数查询获取总页数?

有没有关于如何将这些投入工作的例子?

请分享您在这方面的经验。

谢谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-11-07 01:45:51

您可以使用 LazyDataModel。在此示例中,我使用 Netbeans 创建的 BackBean 和 JpaController 以及“从实体创建 JSF CRUD 页面”(BackBean 必须是 @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;
}

我添加了

    lazyModel.setPageSize(pageSize);

因为除以 0 已知问题 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"
                      >  

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"
                      >  
薄荷梦 2024-11-07 01:45:51

这是我的代码,带有一列(名称列)的过滤器。

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文