Onyx Database: Find By ID Tutorial

You can download the code for this example here:

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

Similarly to the find method, the findById is a handy way of retrieving a single object stored in Onyx Database. This tutorial assumes that you have already stored a hierarchy of related data in an embedded local Onyx Database. In this case, the data stores the stats for all of the team leaders for each team in the NFL for the 2015 season. To see how the data was seeded for this example, see Main.java.

  1. Get an Instance of the PersistenceManager.
  2. Invoke the PersistenceManager#findById method

    This will retrieve the entity object you looking for.

    Notes:
    The findById method expects two arguments:
    The class type of the entity you are requesting
    The Identifier of the entity you are looking for
    One distinction to note is that if the entity you are requesting does not exist, rather than throwing a NoResultsException like the find method does, instead it returns null.
  3. Now, lets confirm that the entity you requested was returned
  4. Make sure to close the factory when you are done with it.
  5. List