IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
    
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerworks > My developerWorks >  Dashboard > WebSphere eXtreme Scale V6.1 User Guide > ... > Basic EntityManager tutorial > Basic EntityManager tutorial step 6
developerWorks
Log In   View a printable version of the current page.
Overview Connect Spaces Forums Wikis
Basic EntityManager tutorial step 6
Added by Chris.D.Johnson, last edited by CarrieMiller on Jun 13, 2007  (view change)
Labels: 

Getting Started Examples Reference API documentation

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.

Order.java
@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) {
        // Cancel all orders that were submitted 1 minute ago
        java.util.Date cancelTime = new java.util.Date(System.currentTimeMillis() - 60000);
        EntityManager em = s.getEntityManager();
        em.getTransaction().begin();
        
        // Create a query that will find the order based on date.  Since
        // we have an index defined on the order date, the query will automatically use it.
        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();
            // Verify that the order wasn't updated by someone else.
            // Since the query used an index, there was no lock on the row.
            if(order != null && order.date.getTime() >= cancelTime.getTime()) {
                for(OrderLine line : order.lines) {
                    // Add the item back to the inventory.
                    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

Wiki Disclaimer and License
© Copyright IBM Corporation 2007,2009. All Rights Reserved.


 
    About IBM Privacy Contact