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 LazyDataModelWSizeNext nosotros ask a service that volition telephone telephone the native query:extends LazyDataModel { someone static lastly long serialVersionUID = -20655217804181429L; world Integer size() { supply getRowCount(); } }
public ListAnd 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.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; }
private LazyDataModelfilteredUsers = 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; }
0 komentar:
Please comment if there are any that need to be asked.