WebSphere eXtreme Scale Product Overview > Tutorials > Tutorials, examples, and samples > Entity manager tutorial



Entity manager tutorial: Creating an entity class


The first step of the entity manager tutorial shows you how to create a local ObjectGrid with one entity by creating an Entity class, registering the entity type with eXtreme Scale, and storing an entity instance into the cache.


Procedure

  1. 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;
    }
    

  2. 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:

    1. Initializes a local eXtreme Scale with an automatically generated name.

    2. Registers the entity classes with the application by using the registerEntities API, although using the registerEntities API is not always necessary.

    3. Retrieves a Session and a reference to the entity manager for the Session.

    4. Associates each eXtreme Scale Session with a single EntityManager and EntityTransaction. The EntityManager is now used.

    5. 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.

    6. 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 associated ObjectGrid Map.

    7. The transaction is then committed, and the entity is included in the ObjectMap.

    8. 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, because Java SE 5 generics capability are not used to ensure that the objectgrid.jar file works on a Java SE 1.4 and later Java virtual machine.



Parent topic

Entity manager tutorial: Overview


Next topic:

Entity manager tutorial: Forming entity relationships


+

Search Tips   |   Advanced Search