Onyx Database: Compound Query Tutorial

You can download the code for this example here:

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

To retrieve a list of entities with compound filter rules use the executeQuery method. This tutorial demonstrates how to query for data in Onyx Database and filter by transitive attributes from another related entity. As an example, we will retrieve a list of "Player" entities and specifically we will search for running backs that have reached 1000 rushingYards or more.

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

  1. Get an Instance of the PersistenceManager.
  2. Create more than one QueryCriteria and chain them into a single compound QueryCriteria

    You can construct a QueryCriteria to do complex queries and filtering. This QueryCriteria below will retrieve a list of players who have a position of "RB" and that also have a child stats entity that has rushingYards >= 1000.

    Notes:
    Notice that we used the and method to join the 2 criterion. and the dot notation indicates that we are filtering by a transitive attribute. In Onyx Database, there is no limit to the depth of nested attributes you can search by.
    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:
    EQUAL
    NOT_EQUAL
    NOT_NULL
    NOT_STARTS_WITH
    STARTS_WITH
    CONTAINS
    LIKE
    MATCHES
    LESS_THAN
    GREATER_THAN
    LESS_THAN_EQUAL
    GREATER_THAN_EQUAL
    IN
    NOT_IN
  3. Create a Query from the compoundCriteria

    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 stats.rushingYards in descending 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 compound criteria of the Query

  5. Now, lets print out the names of all the running backs that have reached 1000 rushingYards or more.
  6. Make sure to close the factory when you are done with it.
  7. Delete Query