Onyx Database: Sorting and Paging Tutorial

You can download the code for this example here:


Onyx Database is capable of sorting and paging. This tutorial demonstrates how to order your results as well as limit the number of results and the starting index of your search. As an example, we will retrieve a list of all "Player" entities in the database and specifically we will order the results by lastName and firstName. We will retrieve the first page of results then the second.

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

  1. Get an Instance of the PersistenceManager.
  2. Create a List of QueryOrders

    A QueryOrder can be used to sort the result set. There is not limit to the number of QueryOrders you can use in a single Query. This list of QueryOrders below will retrieve a list of players and order them by lastName ascending, then firstName ascending.

    The QueryOrder constructor supports 2 arguments:
    attribute The attribute name you are sorting by.
    ascending Boolean flag for sort directions. When false the results will be returned in descending order.
  3. Create a Query with QueryOrder

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

  4. Set the firstRow and maxResults to Implement Paging and Retrieve the First Page
    The firstRow is zero-index, so if you would like the first page set it to 0
  5. Query for the First Page (rows 0-9, records 1-10)
  6. Print the First Page Results
    The output looks like this :
    Page 1: Abdullah, Ameer Ajayi, Jay Allen, Javorius Allen, Kamar Allen, Keenan Amendola, Danny Anderson, C.J. Andrews, Antonio Austin, Tavon Baldwin, Doug
  7. Set the firstRow and maxResults to Retrieve the Second Page
  8. Query for the Second Page (rows 10-19, records 11-20)
  9. Print the Second Page Results
    The output looks like this :
    Page 2: Beasley, Cole Beckham Jr., Odell Bell, Joique Bell, Le'Veon Benjamin, Travis Bernard, Giovani Blount, LeGarrette Blue, Alfred Boldin, Anquan Bortles, Blake
  10. Make sure to close the factory when you are done with it.
  11. Lazy Query