Class JpaLazyDataModel<T>

java.lang.Object
javax.faces.model.DataModel<E>
javax.faces.model.ListDataModel<T>
org.primefaces.model.LazyDataModel<T>
org.primefaces.model.JpaLazyDataModel<T>
Type Parameters:
T - The model class.
All Implemented Interfaces:
Serializable, Iterable<T>, SelectableDataModel<T>

public class JpaLazyDataModel<T> extends LazyDataModel<T> implements Serializable
Basic LazyDataModel implementation with JPA and Criteria API.
See Also:
  • Field Details

    • entityClass

      protected Class<T> entityClass
    • entityManager

      protected SerializableSupplier<javax.persistence.EntityManager> entityManager
    • rowKeyField

      protected String rowKeyField
  • Constructor Details

    • JpaLazyDataModel

      public JpaLazyDataModel()
      For serialization only
    • JpaLazyDataModel

      public JpaLazyDataModel(Class<T> entityClass, SerializableSupplier<javax.persistence.EntityManager> entityManager)
      Constructs a JpaLazyDataModel for usage without enabled selection.
      Parameters:
      entityClass - The entity class
      entityManager - The EntityManager
    • JpaLazyDataModel

      public JpaLazyDataModel(Class<T> entityClass, SerializableSupplier<javax.persistence.EntityManager> entityManager, String rowKeyField)
      Constructs a JpaLazyDataModel with selection support.
      Parameters:
      entityClass - The entity class
      entityManager - The EntityManager
      rowKeyField - The name of the rowKey property (e.g. "id")
    • JpaLazyDataModel

      public JpaLazyDataModel(Class<T> entityClass, SerializableSupplier<javax.persistence.EntityManager> entityManager, javax.faces.convert.Converter converter)
      Constructs a JpaLazyDataModel with selection support, with an already existing Converter.
      Parameters:
      entityClass - The entity class
      entityManager - The EntityManager
      converter - The converter, which will be used for converting the entity to a rowKey and vice versa
  • Method Details

    • count

      public int count(Map<String,FilterMeta> filterBy)
      Description copied from class: LazyDataModel
      Counts the all available data for the given filters. In case of SQL, this would execute a "SELECT COUNT ... WHERE ...". In case you dont use SQL and receive both rowCount and data within a single call, this method should just return 0. You must call LazyDataModel.recalculateFirst(int, int, int) and LazyDataModel.setRowCount(int) in your LazyDataModel.load(int, int, java.util.Map, java.util.Map) method.
      Specified by:
      count in class LazyDataModel<T>
      Parameters:
      filterBy - a map with all filter information (only relevant for DataTable, not for eg DataView)
      Returns:
      the data count
    • load

      public List<T> load(int first, int pageSize, Map<String,SortMeta> sortBy, Map<String,FilterMeta> filterBy)
      Description copied from class: LazyDataModel
      Loads the data for the given parameters.
      Specified by:
      load in class LazyDataModel<T>
      Parameters:
      first - the first entry
      pageSize - the page size
      sortBy - a map with all sort information (only relevant for DataTable, not for eg DataView)
      filterBy - a map with all filter information (only relevant for DataTable, not for eg DataView)
      Returns:
      the data
    • applyFilters

      protected void applyFilters(javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.CriteriaQuery<?> cq, javax.persistence.criteria.Root<T> root, Map<String,FilterMeta> filterBy)
    • applyGlobalFilters

      protected void applyGlobalFilters(Map<String,FilterMeta> filterBy, javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.CriteriaQuery<?> cq, javax.persistence.criteria.Root<T> root, List<javax.persistence.criteria.Predicate> predicates)
    • createPredicate

      protected javax.persistence.criteria.Predicate createPredicate(FilterMeta filter, Field filterField, javax.persistence.criteria.Root<T> root, javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.Expression fieldExpression, Object filterValue)
    • applySort

      protected void applySort(javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.CriteriaQuery<T> cq, javax.persistence.criteria.Root<T> root, Map<String,SortMeta> sortBy)
    • resolveFieldExpression

      protected javax.persistence.criteria.Expression resolveFieldExpression(javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.CriteriaQuery<?> cq, javax.persistence.criteria.Root<T> root, String fieldName)
    • getRowData

      public T getRowData(String rowKey)
      Specified by:
      getRowData in interface SelectableDataModel<T>
      Overrides:
      getRowData in class LazyDataModel<T>
    • getRowKey

      public String getRowKey(T object)
      Specified by:
      getRowKey in interface SelectableDataModel<T>
      Overrides:
      getRowKey in class LazyDataModel<T>
    • convertToType

      protected Object convertToType(Object value, Class valueType)
    • getRowKeyGetter

      protected Method getRowKeyGetter()