Product overview > Tutorials, examples, and samples > ObjectQuery tutorial

ObjectQuery tutorial - step 1

With the following steps, you can continue to develop a local, in-memory ObjectGrid that stores order information for an online retail store using the ObjectMap APIs. You define a schema for the map and run a query against the map.


  1. Create an ObjectGrid with a map schema.

    Create an ObjectGrid with one map schema for the map, then insert an object into the cache and later retrieve it using a simple query.
    public class OrderBean implements Serializable {
        String orderNumber;
        java.util.Date date;
        String customerName;
        String itemName;
        int quantity;
        double price;

  2. Define the primary key.

    The previous code shows an OrderBean object. This object implements the interface because all objects in the cache must (by default) be Serializable.

    The orderNumber attribute is the primary key of the object. The following example program can be run in stand-alone mode. You should follow this tutorial in an Eclipse Java™ project that has the objectgrid.jar file added to the class path.
    package querytutorial.basic.step1;
    import java.util.Iterator;
    public class Application
        static public void main(String [] args) throws Exception
            ObjectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid();
            // Define the schema
            QueryConfig queryCfg = new QueryConfig();
            queryCfg.addQueryMapping(new QueryMapping("Order", OrderBean.class.getName(), 
                        "orderNumber", QueryMapping.FIELD_ACCESS));
            Session s = og.getSession();
            ObjectMap orderMap = s.getMap("Order");
            OrderBean o = new OrderBean();
            o.customerName = "John Smith";
   = new java.util.Date(System.currentTimeMillis());
            o.itemName = "Widget";
            o.orderNumber = "1";
            o.price = 99.99;
            o.quantity = 1;
            orderMap.put(o.orderNumber, o);
            ObjectQuery query = s.createObjectQuery("SELECT o FROM Order o WHERE o.itemName='Widget'");
            Iterator result = query.getResultIterator();
            o = (OrderBean);
            System.out.println("Found order for customer: " + o.customerName);

    This eXtreme Scale application first initializes a local ObjectGrid with an automatically generated name. Next, the application creates a BackingMap and a QueryConfig that defines what Java type is associated with the map, the name of the field that is the primary key for the map, and how to access the data in the object. You then obtain a Session to get the ObjectMap instance and insert an OrderBean object into the map in a transaction.

    After the data is committed into the cache, you can use ObjectQuery to find the OrderBean using any of the persistent fields in the class. Persistent fields are those that do not have the transient modifier. Because you did not define any indexes on the BackingMap, ObjectQuery must scan each object in the map using Java reflection.

What to do next

ObjectQuery tutorial - step 2 demonstrates how an index can be used to optimize the query.

Parent topic:

ObjectQuery tutorial

Next topic:

ObjectQuery tutorial - step 2