1.1.14.4.2.6. fejezet, Hibernate lapozós táblázat PrimeFaces-el

A Hibernate-tel és a PrimeFaces-el így tudtam megjeleníteni egy lapozós táblázatban a perzisztens adatokat.

table.xhtml

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	    		xmlns:ui="http://java.sun.com/jsf/facelets"
	  			xmlns:h="http://java.sun.com/jsf/html"
	  			xmlns:f="http://java.sun.com/jsf/core"
	  			xmlns:p="http://primefaces.org/ui"
				template="/WEB-INF/layouts/standard.xhtml"
				contentType="text/html" encoding="UTF-8">
 
<ui:define name="title">Standard JSR-303 Bean Validation</ui:define>
 
<ui:define name="notes">
	<h5>Táblázat</h5>
	<p>
	  	Rögzített kérelmek.
	</p>
	<hr/>
</ui:define>
 
<ui:define name="content">
 
<h:form prependId="false">
 
    <p:dataTable id="requestTable" var="requestvar" value="#{backBean.lazyModel}"
                 rows="#{backBean.pageSize}" paginator="true"
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                 rowsPerPageTemplate="5,10,15"
                 lazy="true"
                 dynamic="true">
        <f:facet name="header">
            Ajax Pagination
        </f:facet>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Id" />
            </f:facet>
            <h:outputText value="#{requestvar.id}" />
        </p:column>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <h:outputText value="#{requestvar.name}" />
        </p:column>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Organization" />
            </f:facet>
            <h:outputText value="#{requestvar.organization}" />
        </p:column>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Homepage" />
            </f:facet>
            <h:outputText value="#{requestvar.homepage}" />
        </p:column>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Operation type" />
            </f:facet>
            <h:outputText value="#{requestvar.operationType.name}" />
        </p:column>
 
        <p:column>
            <f:facet name="header">
                <h:outputText value="Applicant type" />
            </f:facet>
            <h:outputText value="#{requestvar.applicantType.name}" />
        </p:column>
 
    </p:dataTable>
 
    <p:commandButton value="#{text['exit.label']}" action="exit" immediate="true" />
 
</h:form>
 
 
</ui:define>
 
</ui:composition>

BackBean.java

package com.integrity.dao;
 
import java.util.List;
import java.util.Map;
 
import javax.faces.bean.SessionScoped;
 
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
 
import com.integrity.domain.Request;
 
@SessionScoped
public class BackBean {
    private RequestDAO requestDAO;
 
    private LazyDataModel<Request> lazyModel;
 
    private int pageSize = 5;
 
    private int page = 0;
 
    public BackBean(){
 
    }
 
    public void setRequestDAO(RequestDAO requestDAO){
        this.requestDAO = requestDAO;
        loadData();
    }
 
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
 
    public LazyDataModel<Request> getLazyModel(){
        return this.lazyModel;
    }
 
    public int getPageSize() {
        return pageSize;
    }
 
    public int getPage() {
        return page;
    }
 
    public void setPage(int page){
        this.page = page;
    }
 
    public void loadData() {
        lazyModel = new LazyDataModel<Request>() {
            @Override
            public List<Request> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
                List<Request> result = requestDAO.getRequests(first, pageSize, sortField, sortOrder, filters);
                return result;
            }
        };
 
        lazyModel.setRowCount(requestDAO.getRequestCount());
        lazyModel.setPageSize(pageSize);
    }
}

RequestDAO query pagination

    public List<Request> getRequests(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        if (query == null){
            SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
            Session session = sessionFactory.openSession();
            query = session.createQuery("from Request");
        }
 
        query.setFirstResult(first);
        query.setMaxResults(pageSize);
 
        List<Request> list = query.list();
 
        return list;
    }