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 1
developerWorks
Log In   View a printable version of the current page.
Overview Connect Spaces Forums Wikis
Basic EntityManager tutorial step 1
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.

This tutorial demonstrates how to develop a local, in-memory ObjectGrid that stores order information for an online retail store.

Step 1 explains how to create an Entity class, create an ObjectGrid, register the entity type with the ObjectGrid, and store an entity instance into the cache.

Order.java
@Entity
public class Order
{
    @Id String orderNumber;
    Date date;
    String customerName;
    String itemName;
    int quantity;
    double price;
}

The preceding example shows an Order object. The @Entity annotation is added for the class, which identifies the object as an ObjectGrid entity. All serializable attributes in the object are automatically persisted in the ObjectGrid, unless annotations on the attributes are used to override the attributes.

The orderNumber attribute is annotated with @Id to indicate that this attribute the primary key. The following is an example program that can be issued in stand-alone mode to demonstrate this. Follow this tutorial in an Eclipse Java project that has the objectgrid.jar file added to the class path.

Application.java
package emtutorial.basic.step1;

import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
import com.ibm.websphere.objectgrid.Session;
import com.ibm.websphere.objectgrid.em.EntityManager;

public class Application
{

    static public void main(String [] args)
        throws Exception
    {
        ObjectGrid og = ObjectGridManagerFactory.getObjectGridManager().createObjectGrid();
        og.registerEntities(new Class[] {Order.class});

        Session s = og.getSession();
        EntityManager em = s.getEntityManager();

        em.getTransaction().begin();

        Order o = new Order();
        o.customerName = "John Smith";
        o.date = new java.util.Date(System.currentTimeMillis());
        o.itemName = "Widget";
        o.orderNumber = "1";
        o.price = 99.99;
        o.quantity = 1;

        em.persist(o);
        em.getTransaction().commit();

        em.getTransaction().begin();
        o = (Order)em.find(Order.class, "1");
        System.out.println("Found order for customer: " + o.customerName);
        em.getTransaction().commit();
    }
}

This example is a simple ObjectGrid Hello world application. It first initializes a local ObjectGrid with an automatically generated name. Next, the entity classes are registered with the application by using the registerEntities() API, though this is not always necessary. A Session and a reference to the Session's EntityManager are then retrieved. Each ObjectGrid Session is associated with a single EntityMananager and EntityTransaction. The EntityManager will be used from this point.

The registerEntities() method creates a BackingMap that is called Order, and associates the metadata for the Order object with it. This metadata includes the key and non-key attributes, along with the attribute types and names.

A transaction starts and creates an Order instance. The transaction is populated with some values, and is persisted by using the EntityManager.persist() method, which identifies the entity as waiting to be included in the ObjectGrid Map that is associated with it. The transaction is then committed, and the entity is included in the ObjectMap.

Another transaction is made, and the Order object is retrieved by using the key 1. The type cast on EntityManager.find() is necessary, because Java SE 5 generics capability are not used to ensure that the objectgrid.jar file works on Java SE 1.4 and later Java Virtual Machines (JVM).

Step 1 demonstrates a simple data structure. Complete the next to enhance the data structure to store the information in a different entity.

Next Step

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


 
    About IBM Privacy Contact