See the WebSphere eXtreme Scale Wiki for links to eXtreme Scale Version 7.0 documentation.
If you log in
with your developerWorks ID, you can leave comments and feedback for the development team.
In this tutorial, we develop a local, in-memory ObjectGrid that stores order information for a on-line retail store.
In this step, we expand on the previous step to show how a query can be used to find entities and then update and remove them.
@Entity
public class Order
{
@Id String orderNumber;
@Index java.util.Date date;
@OneToOne(cascade=CascadeType.PERSIST) Customer customer;
@OneToMany(cascade=CascadeType.ALL, mappedBy="order") @OrderBy("lineNumber") List<OrderLine> lines;
}
The Order entity class is the same from the previous example. It still provides the @Index annotation, since our query string will use the date to find the entity. The query engine will use indices whenever they can be utilized.
public static void cancelOrdersUsingQuery(Session s) {
java.util.Date cancelTime = new java.util.Date(System.currentTimeMillis() - 60000);
EntityManager em = s.getEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("SELECT order FROM Order order WHERE order.date >= ?1");
query.setParameter(1, cancelTime);
Iterator<Order> orderIterator = query.getResultIterator();
while(orderIterator.hasNext()) {
Order order = orderIterator.next();
if(order != null && order.date.getTime() >= cancelTime.getTime()) {
for(OrderLine line : order.lines) {
line.item.quantityOnHand += line.quantity;
line.quantity = 0;
}
em.remove(order);
}
}
em.getTransaction().commit();
}
Like the previous example, this method's intent is to cancel all orders that were submitted in the past minute. When we cancel the order, we first find it using a query, add the items in the order back into the inventory and then remove the order and associated line items from the system.
Finding the Entity using a query
Using queries is much simpler than using an index. No ObjectGrid APIs are needed and the underlying format of the data (the Tuple) is not exposed.
To find the entity using a query, we simply create a Query object using the createQuery method of the EntityManager. We use a positional parameter to allow the query to be cached. Once the query is created, we set the parameter, and get the results. The results are return in an Iterator, which we can use to remove the entity. Because the query used an index, we have to re-check the entity to verify that it still satisfies the query result.
Previous Step
© Copyright IBM Corporation 2007,2009. All Rights Reserved.