DDIC Tables are dead, long live Table Entities

Treatment in progress…
You're done! You are in the list.

Whereas before we had to define the creation of our tables via ADT (or worse, via SE11), and then create their associated CDS entities afterwards, it is now possible (and recommended) to create a database table directly as a CDS object.

And when you say CDS, you mean all the associated functionalities, such as associations, annotations and field typing with CDS simple types and CDS enumerated types.

These objects are called Tables Entities, and they’re one of the many exciting new features delivered with ABAP Cloud!

To demonstrate their functionality, we’ll start with an example

Example description

In this example, we’re going to define the Table Entities needed to manage a bakery’s pastry sales.

There will be 3 Table Entities:

  • ZI_BAKERY_ORDERS : Represents the entity root table (the header) of the orders placed
  • ZI_BAKERY_ORDERS_ITEMS : Represents the item table of each order placed
  • ZI_PASTRY : Represents the table of pastries available in the bakery

In order to keep the example simple, we’ll only create these 3 tables. That’s enough to demonstrate the capabilities of Table Entities

Creating entity tables

  • Right clic -> New -> Other ABAP Repository Object
  • Core Data Services -> Data Definition
  • Table Entity (Creation)

Here is the code and explanations for the 3 Table Entities:

1. ZI_BAKERY_ORDERS

@ClientHandling.type: #CLIENT_DEPENDENT
@AbapCatalog.deliveryClass: #APPLICATION_DATA
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Bakery Orders'
define root table entity ZI_BAKERY_ORDERS
{
  key uuid       : uuid;
  deliveryAddress : abap.char( 100 );
  orderDate  : abap.datn;
  //every order is linked to (at least) one or many items 
  _bakeryOrdersItems : composition of exact one to many ZI_BAKERY_ORDERS_ITEMS;
}

This Entity Table is the header for each order placed, and contains the order’s delivery address.

Each header is linked to at least 1 item via the composition relationship performed with the CDS ZI_BAKERY_ORDERS_ITEMS

We also note the use of the latest recommended syntax for the cardinality of associations and compositions. This new syntax improves CDS documentation and ABAP Engine performance by specifying source and target cardinality. To show what this means, I’ve commented on each association and composition to explain their cardinality.

NOTE: As for the syntax of the entity table itself, it’s a clever mix between the creation of a DDIC table, and the creation of a CDS.

  • This defines the table type
  • It allows you to manage authorizations
  • And specify whether the table is client-dependent or not

2. ZI_BAKERY_ORDERS_ITEMS

@ClientHandling.type: #CLIENT_DEPENDENT
@AbapCatalog.deliveryClass: #APPLICATION_DATA
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Bakery Orders Items'
define table entity ZI_BAKERY_ORDERS_ITEMS
{
  key uuid       : uuid;
  key item       : abap.numc( 5 );
      @Semantics.quantity.unitOfMeasure: 'unit'
      quantity   : abap.quan( 3, 0 );
      unit       : abap.unit( 3 );
      @Semantics.amount.currencyCode: 'currency'
      amount     : abap.curr( 6, 2 );
      currency   : abap.cuky( 5 );
      pastryid   : uuid;
      //every item is linked to exactly one parent order
      _bakeryOrders: association to parent ZI_BAKERY_ORDERS on $projection.uuid = _bakeryOrders.uuid; 
      //every item is linked to exactly one pastry. And many items can be linked to the same pastry
      _pastry : association of many to exact one ZI_PASTRY on $projection.pastryid = _pastry.uuid; 
}

This Entity Table corresponds to all items included in the order

. It is linked to its header via an association to parent (no cardinality required for this type of association, as it is always MANY TO EXCAT ONE).

The Entity Table also contains the item’s pastry id. This id is used to create an association with the bakery’s pastry table.

This Entity Table also shows how to manage annotations (in the same way as in CDS) concerning units of measure and currencies linked to associated quantities and amounts.

3. ZI_PASTRY

@ClientHandling.type: #CLIENT_DEPENDENT
@AbapCatalog.deliveryClass: #APPLICATION_DATA
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Pastry'
define table entity ZI_PASTRY
{
  key uuid        : uuid;
      description : abap.char( 50 );
      @Semantics.amount.currencyCode: 'currency'
      unit_amount : abap.curr( 6, 2 );
      currency    : abap.cuky( 5 );
      //A pastry can be linked to many orders (so many orders items) or may not be in any order
      _ordersItems : association of one to many ZI_BAKERY_ORDERS_ITEMS on $projection.uuid = _ordersItems.pastryid; 
}

This Entity Table contains all the bakery’s pastries and is linked to the Order Items Entity Table via the _ordersItems association.

Conclusion

Table Entities are now the future (and the present) for creating database tables, and are set to replace DDIC Tables. According to SAP documentation, a migration from DDIC Tables to CDS table entities will even be proposed.