A etapa a seguir mostra como criar um ObjectGrid com quatro mapas e um esquema para os mapas. Alguns dos mapas mantêm um relacionamento um-para-um (unidirecional) e um relacionamento um-para-muitos (bidirecional). Após criar os mapas, é possível então executar o programa de amostra Application.java para inserir objetos no cache e executar consultas para recuperar esses objetos.
OrderBean.java
public class OrderBean implements Serializable {
String orderNumber;
java.util.Date date;
String customerId;
String itemName;
List<Integer> orderLines;
}
OrderLineBean.java
public class OrderLineBean implements Serializable {
int lineNumber;
int quantity;
String orderNumber;
String itemId;
}
CustomerBean.java
public class CustomerBean implements Serializable{
String id;
String firstName;
String surname;
String address;
String phoneNumber;
}
ItemBean.java
public class ItemBean implements Serializable {
String id;
String description;
long quantityOnHand;
double price;
}
Application.java
public class Application static public void main(String [] args)throws Exception
// Configure programatically
objectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid();
og.defineMap("Order");
og.defineMap("Customer");
og.defineMap("OrderLine");
og.defineMap("Item");
// Define the schema
QueryConfig queryCfg = new QueryConfig();
queryCfg.addQueryMapping(new QueryMapping("Order", OrderBean.class.getName(), "orderNumber", QueryMapping.FIELD_ACCESS));
queryCfg.addQueryMapping(new QueryMapping("Customer", CustomerBean.class.getName(), "id", QueryMapping.FIELD_ACCESS));
queryCfg.addQueryMapping(new QueryMapping("OrderLine", OrderLineBean.class.getName(), "lineNumber", QueryMapping.FIELD_ACCESS));
queryCfg.addQueryMapping(new QueryMapping("Item", ItemBean.class.getName(), "id", QueryMapping.FIELD_ACCESS));
queryCfg.addQueryRelationship(new QueryRelationship(OrderBean.class.getName(), CustomerBean.class.getName(), "customerId", null));
queryCfg.addQueryRelationship(new QueryRelationship(OrderBean.class.getName(), OrderLineBean.class.getName(),
"orderLines", "lineNumber"));
queryCfg.addQueryRelationship(new QueryRelationship(OrderLineBean.class.getName(), ItemBean.class.getName(), "itemId", null));
og.setQueryConfig(queryCfg);
// Get session and maps;
Session s = og.getSession();
ObjectMap orderMap = s.getMap("Order");
ObjectMap custMap = s.getMap("Customer");
ObjectMap itemMap = s.getMap("Item");
ObjectMap orderLineMap = s.getMap("OrderLine");
// Add data
s.begin();
CustomerBean aCustomer = new CustomerBean();
aCustomer.address = "Main Street";
aCustomer.firstName = "John";
aCustomer.surname = "Smith";
aCustomer.id = "C001";
aCustomer.phoneNumber = "5555551212";
custMap.insert(aCustomer.id, aCustomer);
// Insert an order with a reference to the customer, but without any OrderLines yet.
// Because we are using CopyMode.COPY_ON_READ_AND_COMMIT, the
// insert won't be copied into the backing map until commit time, so
// the reference is still good.
OrderBean anOrder = new OrderBean();
anOrder.customerId = aCustomer.id;
anOrder.date = new java.util.Date();
anOrder.itemName = "Widget";
anOrder.orderNumber = "1";
anOrder.orderLines = new ArrayList();
orderMap.insert(anOrder.orderNumber, anOrder);
ItemBean anItem = new ItemBean();
anItem.id = "AC0001";
anItem.description = "Description of widget";
anItem.quantityOnHand = 100;
anItem.price = 1000.0;
itemMap.insert(anItem.id, anItem);
// Create the OrderLines and add the reference to the Order
OrderLineBean anOrderLine = new OrderLineBean();
anOrderLine.lineNumber = 99;
anOrderLine.itemId = anItem.id;
anOrderLine.orderNumber = anOrder.orderNumber;
anOrderLine.quantity = 500;
orderLineMap.insert(anOrderLine.lineNumber, anOrderLine);
anOrder.orderLines.add(Integer.valueOf(anOrderLine.lineNumber));
anOrderLine = new OrderLineBean();
anOrderLine.lineNumber = 100;
anOrderLine.itemId = anItem.id;
anOrderLine.orderNumber = anOrder.orderNumber;
anOrderLine.quantity = 501;
orderLineMap.insert(anOrderLine.lineNumber, anOrderLine);
anOrder.orderLines.add(Integer.valueOf(anOrderLine.lineNumber));
s.commit();
s.begin();
// Find all customers who have ordered a specific item.
ObjectQuery query = s.createObjectQuery("SELECT c FROM Order o JOIN o.customerId as c WHERE o.itemName='Widget'");
Iterator result = query.getResultIterator();
aCustomer = (CustomerBean) result.next();
System.out.println("Found order for customer: " + aCustomer.firstName + " " + aCustomer.surname);
s.commit();
s.begin();
// Find all OrderLines for customer C001.
// The query joins are expressed on the foreign keys.
query = s.createObjectQuery("SELECT ol FROM Order o JOIN o.customerId as c JOIN o.orderLines as ol WHERE c.id='C001'");
result = query.getResultIterator();
System.out.println("Found OrderLines:");
while(result.hasNext()) {
anOrderLine = (OrderLineBean) result.next();
System.out.println(anOrderLine.lineNumber + ", qty=" + anOrderLine.quantity);
}
// Close the session (optional in Version 7.1.1
and later) for improved performance
s.close();
}
}
<?xml version="1.0" encoding="UTF-8"?><objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://ibm.com/ws/objectgrid/config
../objectGrid.xsd"xmlns="http://ibm.com/ws/objectgrid/config">
<objectGrids>
<objectGrid name="CompanyGrid">
<backingMap name="Order"/>
<backingMap name="Customer"/>
<backingMap name="OrderLine" />
<backingMap name="Item" />
<querySchema>
<mapSchemas>
<mapSchema
mapName="Order"
valueClass="com.mycompany.OrderBean"
primaryKeyField="orderNumber"
accessType="FIELD"/>
<mapSchema
mapName="Customer"
valueClass="com.mycompany.CustomerBean"
primaryKeyField="id"
accessType="FIELD"/>
<mapSchema
mapName="OrderLine"
valueClass="com.mycompany.OrderLineBean"
primaryKeyField="
lineNumber"
accessType="FIELD"/>
<mapSchema
mapName="Item"
valueClass="com.mycompany.ItemBean"
primaryKeyField="id"
accessType="FIELD"/>
</mapSchemas>
<relationships>
<relationship
source="com.mycompany.OrderBean"
target="com.mycompany.CustomerBean"
relationField="customerId"/>
<relationship
source="com.mycompany.OrderBean"
target="com.mycompany.OrderLineBean"
relationField="orderLines"
invRelationField="lineNumber"/>
<relationship
source="com.mycompany.OrderLineBean"
target="com.mycompany.ItemBean"
relationField="itemId"/>
</relationships>
</querySchema>
</objectGrid>
</objectGrids>
</objectGridConfig>