Onyx Database: Lazy Query Tutorial

You can download the code for this example here:

OnyxDevTools/onyx-database-examples/querying/LazyQueryExample.

Onyx Database supports lazy retrieval of entities using the executeLazyQuery method. Calling this method will return a list with record references rather than the entire entity. The references are then used to hydrate the results when referenced through the List#get method. This is helpful when implementing infinite scrolling or reducing the packet size and memory footprint when displaying a list of results or retrieving a parent entity with an enormous child collection that has FetchPolicy.LAZY. See Fetch Policy for more information.
If you are using Android or JavaFx, you can easily bind the LazyQueryCollection to a grid view.

This tutorial demonstrates how to lazily query for data in Onyx Database. As an example, we will execute a lazy query to fetch a list of all "Player" entity references, and then hydrate them one by one.

To see how the data was seeded for this example, see Main.java.

  1. Get an Instance of the PersistenceManager.
  2. Create a Simple Query to Include All Records

    This example uses one of the many constructors that are available.

    Notes:
    Notice that we have passed the criteria and a QueryOrder to sort by firstName in ascending order.
    The Query constructors supports the following arguments:
    entityType: The class type of the entities you are requesting
    criteria The QueryCriteria you constructed in the last step
    queryOrder Gives you the ability to order by field and direction (direction is set to ASC by default)
    selections A list of attributes you want to reduce the result rows down to
    updates List of AttributeUpdates. This enables you to do bulk update queries
    criteria The QueryCriteria you constructed in the last step
    The Query also has the following properties that can be set after construction
    maxResults The max number of rows returned
    firstRow The starting index of the first record returned (using zero based counting)
    partition Specifies the partition to query the data from. If not specified, the query will span over all partitions.
    resultCount After executing a query you can call getResultCount() to get the total number of results that meet the query's critiera.
  3. Invoke the PersistenceManager#executeLazyQuery method

    This will retrieve a LazyQueryCollection which extends the List interface.

  4. Now, lets get at print out all of entites in the LazyQueryCollection
    Notes:
    Because the List that is returned is not fully hydrated, you will cannot use the iterator or forEach methods or use the for-each loop construct. You must use a for loop using the count and call the get method on the List to retrieve the record when you want to hydrate the reference.
  5. Make sure to close the factory when you are done with it.
  6. Partitioning