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

Customer.java
@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:

Order.java
@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:

Application.java
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

Wiki Disclaimer and License
© 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?

Posted by cylus at Feb 13, 2007 01:54 | Permalink

I'm in the process of adding a session expanding on this example. All entities in a schema must have a single "root". In this example, I'll introduce a new entity: District, which will be used to partition all of the other entities. If entities are not access through a root entity, then an exception will be thrown. I'm going to start updating the [Defining an EntityManager Schema] page to include the specifics. There is some information here as well: Introduction to partitioning

Posted by Chris.D.Johnson at Mar 26, 2007 18:09 | Permalink

The previous comment should have a link to: Defining an Entity Schema

Posted by Chris.D.Johnson at Mar 26, 2007 18:10 | Permalink

 
    About IBM Privacy Contact