Primefaces DataTable + JPA/Hibernate分页

ber*_*tie 6 hibernate jpa primefaces jsf-2 jpa-2.0

如果我能以某种方式将这两个框架结合在一起,那将会非常酷.

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

也许有一些机制,primefaces组件也可以从另一个JPA选择计数查询中获取总页数?

有没有关于如何将这些付诸实践的例子?

请分享您的经验.

谢谢 !

Mir*_*chi 11

您可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

我已经添加

    lazyModel.setPageSize(pageSize);
Run Code Online (Sandbox Code Playgroud)

由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"
                      >  
Run Code Online (Sandbox Code Playgroud)

  • 它也可以与@ViewScoped一起使用. (4认同)