I implemented LazyDataModel using primefaces tutorial and facing the problem.
If I return list.sublist(...) - everything works fine, but I get Exception:
java.io.NotSerializableException: java.util.ArrayList$SubList.
I found out that List returned by subList() method is an instance of 'RandomAccessSubList' which is not serializable. And I tried to create new List.
If I return new ArrayList(list.sublist(...)) - Exception dissapears, but load() method is not being called and I can't fetch next chunk of elements.
Is there a way to get rid of this annoying Exception?
UPDATE:
public class LazyApartmentDataModel extends LazyDataModel<MyEntity> implements Serializable{
private EntityManager em;
private String queryText;
private SearchCriteria searchCriteria;
public LazyApartmentDataModel() {}
public LazyApartmentDataModel(EntityManager em, SearchCriteria searchCriteria, int totalSize) {
this.em = em;
prepareQueryText(searchCriteria);
this.searchCriteria = searchCriteria;
this.setRowCount(totalSize);
}
private void prepareQueryForGrid(SearchCriteria searchCriteria) {
StringBuilder query = new StringBuilder("select ... from ... where ... ");
this.queryText = query.toString();
}
@Override
public List<MyEntity> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
List<MyEntity> list;
Query q = em.createQuery(this.queryText);
// Paginate
q.setFirstResult(first);
q.setMaxResults(pageSize);
list = query.getResultList();
return list.subList(0,list.size());
}
}