How To Implement A Lazydatamodel Amongst Size Inwards Primefaces

This solution is ideal if you lot accept a listing of entities or a named queries that supply a listing of entities.

For example let's nation nosotros accept an entity user together with nosotros desire to supply all the users alongside role=x.
@NamedQueries({ @NamedQuery(name = "User.listUsersByRoles", inquiry = "" + "SELECT u FROM User u" + " LEFT JOIN u.roles equally role" + " WHERE role.name IN (:roleNames)) }) world cast User implents Serializeable { } 

Then nosotros ask to extend primefaces's LazyDataModel class, overriding the size method.

import org.primefaces.model.LazyDataModel;  world cast LazyDataModelWSize extends LazyDataModel {   someone static lastly long serialVersionUID = -20655217804181429L;   world Integer size() {   supply getRowCount();  } } 
Next nosotros ask a service that volition telephone telephone the native query:
public List listUsersByRoleList roleNames) {  List users = null;   essay {   users = entityManager.createNamedQuery("User.listUsersByRoles").setParameter("roleNames", roleNames).getResultList();  } pick out grip of (Exception e) {   log.error("null {}", e.getMessage());  }   supply users; } 
And finally a edible bean that volition telephone telephone the service. Let's nation nosotros desire to inquiry all the users alongside work admin together with hr.
private LazyDataModel filteredUsers = null;   world LazyDataModel getFilteredLazyDataModel() {   if (filteredUsers != null) {   supply filteredUsers;  }   filteredUsers = novel LazyDataModelWSize() {   someone static lastly long serialVersionUID = 1L;    @Override   world List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map loadingFilters) {     List entities = null;    entities = userService.listUsersByRoles(Arrays.asList("ADMIN", "HR"));    setRowCount(entities.size());     supply entities.subList(first, (first + pageSize) > entities.size() ? entities.size() : (first + pageSize));   }  };   supply filteredUsers; } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.