Onyx Database: Query Tutorial

You can download the code for this example here:


To retrieve a list of entities with complex filter rules use the executeQuery method. This tutorial demonstrates how to query for data in Onyx Database. As an example, we will retrieve a list of "Player" entities and specifically we will search for quarterbacks with a firstName that starts with C and order them by lastName in ascending order.

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

  1. Get an Instance of the PersistenceManager.
  2. Create a QueryCriteria

    You can construct a QueryCriteria to do complex queries and filtering. This QueryCriteria below will retrieve a list of players who have a lastName starting with "C" and have a position of "QB".

    The QueryCriteria constructor supports 3 arguments:
    attribute: The field you are filtering by
    criteriaEnum: The comparison operator to use. We recommend using the QueryCriteriaOperator statics
    value: The value to filter by
    QueryCriteriaOperators include:
  3. Create a Query

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

    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.
  4. Invoke the PersistenceManager#executeQuery method

    This will retrieve a list of entities according to the criteria of the Query

  5. Now, lets print out the names of all the quarterbacks that have a lastName starting with "C".
  6. Make sure to close the factory when you are done with it.
  7. Compound Query