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 illustrates how to develop a local, in-memory ObjectGrid that stores order information for an online retail store.
Step 2 demonstrates how to create two entity classes with a relationship between the two, register the entities with the ObjectGrid, and store the entity instances into the cache.
The following example is the new Customer entity, which is used to store customer details independently from the Order object:
@Entity
public class Customer
{
@Id String id;
String firstName;
String surname;
String address;
String phoneNumber;
}
The following example illustrates the new Order object, which is similar to the Order object that is used in step 1:
@Entity
public class Order
{
@Id String orderNumber;
Date date;
@ManyToOne(cascade=CascadeType.PERSIST) Customer customer;
String itemName;
int quantity;
double price;
}
This is similar to step 1, except a reference to a Customer object replaces the customerName attribute. The reference has an annotation that indicates this is a many-to-one relationship, in which each order has one customer, but multiple orders might reference the same customer. The cascade annotation modifier indicates that if the EntityManager persists the Order object, it must also persist the Customer object. If you choose to not set the cascade persist option, which is the default option, you must manually persist the Customer object with the Order object.
The ObjectGrid now has two maps defined. Each map is defined for a specific entity, and one entity is named Order and the other is named Customer. The following example is the application that illustrates how to store and retrieve a customer order:
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();
Customer cust = new Customer();
cust.address = "Main Street";
cust.firstName = "John";
cust.surname = "Smith";
cust.id = "C001";
cust.phoneNumber = "5555551212";
Order o = new Order();
o.customer = cust;
o.date = new java.util.Date();
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.customer.firstName + " " + o.customer.surname);
em.getTransaction().commit();
}
}
This application is similar to the one that is featured in step 1. In the preceding example, only a single class Order is registered. ObjectGrid detects and automatically includes the reference to the Customer entity, and a Customer instance for John Smith is created and referenced from the new Order object. As a result, the new customer is automatically persisted, because the relationship between two orders includes the cascade modifier, which requires that each object be persisted. When the Order object is found, the EntityManager automatically finds the associated Customer object and inserts a reference to it.
The next step demonstrates a more advanced relationship that exists between four entities.
Previous Step Next Step
© Copyright IBM Corporation 2007,2009. All Rights Reserved.
Let us say we have Entities A & B and they have 1-1 relationship. I create A at some point in time (gets created in Partition P1) and later B in another point in time (in a different process, get created in Partition P2). There is a third process which looks for A and looks for B. If it puts B's reference in A and persists A (transaction complete) and A's reference in B and persists B (transaction complete), will I get an error? Further if I retrieve either A or B will I also be able to get the other object?