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.
An entity instance has the following states:
- New: A newly created entity instance that does not exist in the ObjectGrid cache.
- Managed: The entity instance exists in the ObjectGrid cache and is retrieved or persisted using EntityManager. An entity must be associated with an active transaction to be in the managed state.
- Detached: The entity instance exists in the ObjectGrid cache, but is no longer associated with an active transaction.
- Removed: The entity instance is removed, or is scheduled to be removed, from the ObjectGrid cache when the transaction is flushed or committed.
- Invalidated:
The entity instance is invalidated, or is scheduled to be invalidated, from the ObjectGrid cache when the transaction is flushed or committed.
When entities change from state to state, you can invoke life-cycle, call-back methods. To configure such methods, see the
Entity listeners and callback methods topic.
The following sections further describe the meanings of New, Managed, Detached, Removed and Invalidated states as the states apply to an entity.
Finding an entity
EntityManager supports the find operation with and without transaction demarcation:
em.getTransaction().begin();
Order o=(Order)em.find(Order.class,"ASD100");
em.getTransaction().commit()
Order o=(Order)em.find(Order,class,"ASD100");
Persisting an entity
With the EntityManager Interface, you can persist entities by using the persist method. You can persist only a new entity instance, and you must describe it with a sample code. The Basic EntityManager tutorial step 2 example has the following entities: Customer, Item, OrderLine and Order.
Order newOrder = createNewOrder(); em.getTransaction().begin();
Customer customer10=(Customer)em.find(Customer.class, "10"); newOrder.customer=customer10;
em.persist(newOrder);
em.getTransaction().commit();
Order newOrder2 = createNewOrder(); em.getTransaction().begin();
newOrder.customer=customer10; em.persist(newOrder2);
em.getTransaction().commit();
Note the following items when persisting an entity:
- You can persist only new entities. The entities are in the managed state after the em.persist(object) is called. The entity stays in the managed state, until the transaction is either committed or rolled back. Essentially, it is an insert operation.
- The persist operation is ignored for managed entities, and any value changes are updated. If dirty, it is an update operation.
- If CascadeType is PERSIST or ALL, the operation is cascaded to the associations.
- If a newly created Entity or a detached Entity is persisted, an EntityExistsException exception results if the key exists in the ObjectGrid Cache. Essentially, it is an insert operation, which results in an exception due to a duplicate key.
Removing or invalidating an Entity
Removing or invalidating an entity has similar semantics as the persist method. Removing an entity removes the entity from ObjectGrid and the back-end. Invalidating an entity only removes the entity from ObjectGrid, and does not invoke the Loader plug-ins.
Entity invalidation is new for ObjectGrid in WebSphere eXtreme Scale Version 6.1.0.3.
- To remove an entity, the entity must be in managed state.
- If CascadeType is REMOVE or ALL, the remove operation is cascaded to the associations.
- If CascadeType is INVALIDATE or ALL, the invalidate operation is cascaded to the associations.
- An IllegalArgumentException exception can be thrown if removing or invalidating a detached entity.
- If an entity is new, the remove or invalidate operation ignores the entity. Cascading is honored, based on CascadeType.
em.getTransaction().begin();
Order o=(Order)em.find(Order.class,"ASD100"); em.remove(o);
em.getTransaction().commit();
Order o=(Order)em.find(Order.class,"ASD100"); em.getTransaction().begin();
em.remove(o); em.getTransaction().commit();
© Copyright IBM Corporation 2007,2009. All Rights Reserved.