How to implement a LazyDataModel with size in Primefaces
Tech-Today

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 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;
}




- Hibernate - Get Classes That Referenced A Given Entity
There are times when we want to list the entities that referenced (via foreign keys) a given entity x. For example, when deleting entity x that is being referenced, it will throw a generic ConstraintViolationException. Oftentimes, we need to display this...

- Hibernate Search Faceting
With the document available from Hibernate Search (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting), we should be able to implement a faceting example that returns the faceting field and its count. In the example...

- How To Use Shiro With Jdbc On Javaee6 And Glassfish
Before you proceed with this tutorial, it's best to from this link to know how to setup a JavaEE6 project with Shiro integrated: http://czetsuya-tech.blogspot.com/2012/10/how-to-integrate-apache-shiro-with.html From there we will focus on the changes:...

- Create A Primary Key Column With Sequence Generated Value In Hibernate
For example you have a class User: package org.kalidad.seamexercises.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.SequenceGenerator; ...

- Entity Framework Inserting Or Updating A Table With Foreign Key
For example you have 2 lookup entities: Country and Province And Your Person Entity has country and province field. In short both CountryId and ProvinceId are foreign keys to Person table. How to setup the Insert Statement (Update is almost the same,...



Tech-Today








.