Create a local ObjectGrid with one entity by creating an
Entity class, registering the entity type, and storing an entity instance
into the cache.
Procedure
- Create the Order object. To identify the object
as an ObjectGrid entity, add the @Entity annotation. When you add
this annotation, all serializable attributes in the object are automatically
persisted in eXtreme Scale,
unless you use annotations on the attributes to override the attributes. The orderNumber attribute
is annotated with @Id to indicate that this
attribute is the primary key. An example of an Order
object follows:
Order.java
@Entity
public class Order
{
@Id String orderNumber;
Date date;
String customerName;
String itemName;
int quantity;
double price;
}
- Run the eXtreme Scale Hello
World application to demonstrate the entity operations. The
following example program can be issued in stand-alone mode to demonstrate
the entity operations. Use this program in an Eclipse Java™ project that has the objectgrid.jar file
added to the class path. An example of a simple Hello
world application that uses eXtreme Scale follows:
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 application performs
the following operations: - Initializes a local eXtreme Scale with
an automatically generated name.
- Registers the entity classes with the application by using the registerEntities API,
although using the registerEntities API is not
always necessary.
- Retrieves a Session and a reference to the entity manager for
the Session.
- Associates each eXtreme Scale Session
with a single EntityManager and EntityTransaction. The EntityManager
is now used.
- The registerEntities method creates a BackingMap
object that is called Order, and associates the metadata for the Order
object with the BackingMap object. 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. The transaction is then persisted by
using the EntityManager.persist method, which identifies
the entity as waiting to be included in the associated map.
- The transaction is then committed, and the entity is included
in the ObjectMap instance.
- Another transaction is made, and the Order object is retrieved
by using the key 1. The type cast on the EntityManager.find method
is necessary. The Java SE 5
capability is not used to ensure that the objectgrid.jar file
works on a Java SE Version
5 and later Java virtual
machine.