Product overview > Tutorials, examples, and samples > REST data services sample and tutorial > Enable the REST data service



Retrive and updating data with REST

The OData protocol requires that all entities can be addressed by their canonical form. This means that each entity must include the key of the partitioned, root entity, the schema root.

The following is an example of how to use the association from a root entity to address a child in :

/Customer('ACME')/order(100)

In WCF Data Services, the child entity must be directly addressable, meaning that the key in the schema root must be a part of the key of the child: /Order(customer_customerId='ACME', orderId=100). This is achieved by creating an association to the root entity where the one-to-one or many-to-one association to the root entity is also identified as a key. When entities are included as part of the key, the attributes of the parent entity are exposed as key properties.

Figure 1. Customer and Order entity schema diagram

Customer and Order entity schema diagram

The Customer/Order entity schema diagram illustrates how each entity is partitioned using the Customer. The Order entity includes the Customer as part of its key and is therefore directly accessible. The REST data service exposes all key associations as individual properties: Order has customer_customerId and OrderDetail has order_customer_customerId and order_orderId.

Use the EntityManager API, you can find the Order using the Customer and order id:

transaction.begin();
// Look-up the Order using the Customer.  We only include the Id
// in the Customer class when building the OrderId key instance.
Order order = (Order) em.find(Order.class, 
    new OrderId(100, new Customer('ACME')));
...
transaction.commit();

When using the REST data service, the Order can be retrieved with either of the URLs:

The customer key is addressed using the attribute name of the Customer entity, an underscore character and the attribute name of the customer id: customer_customerId.

An entity can also include a non-root entity as part of its key if all of the ancestors to the non-root entity have key associations to the root. In this example, OrderDetail has a key-association to Order and Order has a key-association to the root Customer entity. Using the EntityManager API:

transaction.begin();
// Construct an OrderDetailId key instance.  It includes
// The Order and Customer with only the keys set. 
Customer customerACME = new Customer("ACME");
Order order100 = new Order(100, customerACME);
OrderDetailId orderDetailKey = 
    new OrderDetailId(order100, "COMP");
OrderDetail orderDetail = (OrderDetail) 
    em.find(OrderDetail.class, orderDetailKey); 
...

The REST data service allows addressing the OrderDetail directly:

/OrderDetail(productId=500, order_customer_customerId='ACME', order_orderId =100)

The association from the OrderDetail entity to the Product entity has been broken to allow partitioning the Orders and Product inventory independently. The OrderDetail entity stores the category and product id instead of a hard relationship. By decoupling the two entity schemas, only one partition is accessed at a time.

The Category and Product schema illustrated in the diagram shows that the root entity is Category and each Product has an association to a Category entity. The Category entity is included in the Product identity. The REST data service exposes a key property: category_categoryId which allows directly addressing the Product.

Because Category is the root entity, in a partitioned environment, the Category must be known in order to find the Product. Using the EntityManager API, the transaction must be pinned to the Category entity prior to finding the Product.

Use the EntityManager API:

transaction.begin();
// Create the Category root entity with only the key.  This
// allows us to construct a ProductId without needing to find
// The Category first.  The transaction is now pinned to the 
// partition where Category "COMP" is stored.
Category cat = new Category("COMP");
Product product = (Product) em.find(Product.class, 
    new ProductId(500, cat)); 
...

The REST data service allows addressing the Product directly:

/Product(productId=500, category_categoryId='COMP')


Parent topic:

Enable the REST data service


Related concepts

Scalable data model in eXtreme Scale


Related tasks

Start a stand-alone data grid for REST data services

Start a data grid for REST data services in WAS