PrimeFaces - DataTable Lazy Data Loading with Pagination and Sorting

[Updated: Aug 16, 2017, Created: Jun 11, 2017]

This is an example of PrimeFaces lazy DataTable, which has pagination and sorting as well. We are going to use JPA, Hibernate and in-memory H2 database to persist/load data.

The view


<p:dataTable var="employee" value="#{employeeBean.model}"
             paginator="true" rows="10"
                     "{RowsPerPageDropdown} {FirstPageLink}
                     {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
             rowsPerPageTemplate="5,10,15" lazy="true">
    <p:column headerText="Id" sortBy="#{}">
        <h:outputText value="#{}"/>

    <p:column headerText="Name" sortBy="#{}">
        <h:outputText value="#{}"/>

    <p:column headerText="Phone Number" sortBy="#{employee.phoneNumber}">
        <h:outputText value="#{employee.phoneNumber}"/>

    <p:column headerText="Address" sortBy="#{employee.address}">
        <h:outputText value="#{employee.address}"/>

The managed bean

public class EmployeeBean {
  public LazyDataModel<Employee> getModel(){
      return dataModel;
public class Employee {
  @SequenceGenerator(name = "eSGen", sequenceName = "employeeSeq",
          initialValue = 50)
  @GeneratedValue(generator = "eSGen")
  private long id;
  private String name;
  private String phoneNumber;
  private String address;

LazyDataModel implementation

public class EmployeeLazyDataModel extends LazyDataModel<Employee> {

  public EmployeeLazyDataModel(){
  public List<Employee> load(int first, int pageSize, String sortField,
                             SortOrder sortOrder, Map<String, Object> filters) {

      List<Employee> list = DataService.INSTANCE.getEmployeeList(first, pageSize, sortField, sortOrder);
      return list;


public enum DataService {
  private final EntityManagerFactory emf =

  DataService() {
      //persisting some data in database
      EntityManager em = emf.createEntityManager();

      DataFactory dataFactory = new DataFactory();
      for (int i = 1; i < 100; i++) {
          Employee employee = new Employee();
          employee.setPhoneNumber(String.format("%s-%s-%s", dataFactory.getNumberText(3),
          employee.setAddress(dataFactory.getAddress() + "," + dataFactory.getCity());


  public List<Employee> getEmployeeList(int start, int size,
                                        String sortField, SortOrder sortOrder) {
      EntityManager em = emf.createEntityManager();
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
      Root<Employee> r = q.from(Employee.class);
      CriteriaQuery<Employee> select =;
      if (sortField != null) {
          q.orderBy(sortOrder == SortOrder.DESCENDING ?
                  cb.asc(r.get(sortField)) : cb.desc(r.get(sortField)));

      TypedQuery<Employee> query = em.createQuery(select);
      List<Employee> list = query.getResultList();
      return list;

  public int getEmployeeTotalCount() {
      EntityManager em = emf.createEntityManager();
      Query query = em.createQuery("Select count( From Employee e");
      return ((Long) query.getSingleResult()).intValue();

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Example Project

Dependencies and Technologies Used :

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • h2 1.4.193: H2 Database Engine.
  • hibernate-core 5.2.8.Final: The core O/RM functionality as provided by Hibernate.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

DataTable Lazy Pagination With Sorting Select All Download
  • data-table-lazy-pagination-with-sorting
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF
        • webapp

See Also