Primefaces 数据表 + JPA/Hibernate 分页 [英] Primefaces DataTable + JPA / Hibernate Pagination

查看:23
本文介绍了Primefaces 数据表 + JPA/Hibernate 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

点击 Primefaces Datatable 上的 next 或 prev 按钮将触发查询,使用 JPA 限制查询结果.

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

也许通过某种机制,primefaces 组件也可以从另一个 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.

谢谢!

推荐答案

您可以使用 LazyDataModel.在此示例中,我使用 Netbeans 创建的 BackBean 和 JpaController,并带有从实体创建 JSF CRUD 页面"(BackBean 必须为 @SessionScoped)

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

我已经添加了

    lazyModel.setPageSize(pageSize);

因为除以 0 知道问题 http://code.google.com/p/primefaces/issues/detail?id=1544

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

这篇关于Primefaces 数据表 + JPA/Hibernate 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆