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

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

问题描述



点击Primefaces Datatable上的下一个或上一个按钮将会触发该查询使用JPA限制查询结果。



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



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



请分享您的经验。



谢谢!

解决方案

您可以使用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){

//排序和过滤信息不用于演示目的,只是返回随机虚拟数据

列表< Car> result = new ArrayList< Car>();

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

返回结果;
}
};
$ b $ **
*在实际应用中,这个数字应该通过投影查询
* /
lazyModel.setRowCount(getJpaController()。getCarCount( ));
lazyModel.setPageSize(pageSize);
}

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

我已经添加了

  lazyModel.setPageSize(pageSize); 

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

 < p:dataTable var =itemvalue =#{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
>


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 !

解决方案

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

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

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