Class MemoryEfficientLazyEntityList

All Implemented Interfaces:
EntityList, AutoCloseable, Iterable<Entity>

public class MemoryEfficientLazyEntityList extends AbstractLazyEntityList implements AutoCloseable
An implementation of EntityList that loads the entities in batches (similar to LazyEntityList), but uses a new Context for every page. When a new page is loaded, the old Context of the previous page will be closed. This implementation is efficient when the data is accessed in the order of the list. It's very inefficient if the elements are accessed randomly. In addition, entities should only be used within the loop, because not all operations are supported when the is closed.
  • Constructor Details

  • Method Details

    • get

      public Entity get(int index, boolean eager) throws PersistException
      Description copied from interface: EntityList
      Get the entity at the specified index.
      Specified by:
      get in interface EntityList
      Parameters:
      index - The index.
      eager - true to initialize the entity right away if it is a lazily loaded proxy.
      Returns:
      The entity at the specified index potentially in unusable state, or null.
      Throws:
      PersistException
    • getManager

      public EntityManager getManager()
      Description copied from interface: EntityList
      Get the entity manager.
      Specified by:
      getManager in interface EntityList
      Returns:
      The entity manager.
    • getFirstEntity

      public Entity getFirstEntity()
      Specified by:
      getFirstEntity in interface EntityList
      Returns:
      the first entity or null
    • toList

      public List<Entity> toList()
      Calling toList() will lose the memory management functionality, so we just load all entities from the thread context.
      Specified by:
      toList in interface EntityList
      Overrides:
      toList in class AbstractEntityList
    • contextCreated

      protected void contextCreated(Context context)
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable