How to implement a LazyDataModel with size in Primefaces

This solution is ideal if you have a list of entities or a named queries that return a list of entities. For example let's say we have a...

This solution is ideal if you have a list of entities or a named queries that return a list of entities.

For example let's say we have an entity user and we want to return all the users with role=x.
@NamedQueries({ @NamedQuery(name = "User.listUsersByRoles", query = ""
+ "SELECT u FROM User u"
+ " LEFT JOIN u.roles as role"
+ " WHERE role.name IN (:roleNames)) })
public class User implents Serializeable {
}

Then we need to extend primefaces's LazyDataModel class, overriding the size method.

import org.primefaces.model.LazyDataModel;

public class LazyDataModelWSize extends LazyDataModel {

private static final long serialVersionUID = -20655217804181429L;

public Integer size() {
return getRowCount();
}
}
Next we need a service that will call the native query:
public List listUsersByRoleList roleNames) {
List users = null;

try {
users = entityManager.createNamedQuery("User.listUsersByRoles").setParameter("roleNames", roleNames).getResultList();
} catch (Exception e) {
log.error("null {}", e.getMessage());
}

return users;
}
And finally a bean that will call the service. Let's say we want to query all the users with role admin and hr.
private LazyDataModel filteredUsers = null; 

public LazyDataModel getFilteredLazyDataModel() {
if (filteredUsers != null) {
return filteredUsers;
}

filteredUsers = new LazyDataModelWSize() {
private static final long serialVersionUID = 1L;

@Override
public 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());

return entities.subList(first, (first + pageSize) > entities.size() ? entities.size() : (first + pageSize));
}
};

return filteredUsers;
}

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to implement a LazyDataModel with size in Primefaces
How to implement a LazyDataModel with size in Primefaces
toztech
https://toztech.blogspot.com/2017/01/how-to-implement-lazydatamodel-with.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2017/01/how-to-implement-lazydatamodel-with.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content