1

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());
}
}
Anton Petrovskyi
  • 442
  • 5
  • 18
  • 1
    Why would you need a sub list in the first place? You have a XY problem. Please also read my comment on your previous question on the `LazyDataModel`. – Jasper de Vries Oct 12 '18 at 11:32
  • I already changed the way I limit my result as you recommended, you may see in update section of this question that I use setFirstResult and setMaxResults. But Exception did not disappear. Thank you for your help! – Anton Petrovskyi Oct 12 '18 at 19:40

1 Answers1

1

Ok, I found the problem. One of Primefaces' JS files was missing.

Anton Petrovskyi
  • 442
  • 5
  • 18