Week 2 Unit 2: Creating the Database Tables
In the present hands-on exercise, you will define the travel and the booking interface views (aka BO views) of the CDS data model for your travel app. As preparation step for week 3, we will directly define the business object structure - i.e. the composition tree – in this exercise.
You can watch week 2 unit 3: Creating the Core Data Services (CDS) Data Model on the openSAP platform.
Hints and Tips
Speed up the typing by making use of the Code Completion feature (shortcut Ctrl+Space) and the prepared code snippets provided. You can easily open an object with the shortcut Ctrl+Shift+A, format your source code using the Pretty Printer feature Shift+F1 and toggle the fullscreen of the editor using the shortcut Ctrl+M.A great overview of ADT shortcuts can be found here: Useful ADT Shortcuts
Please note that the placeholder
####
used in object names in the exercise description must be replaced with the suffix of your choice during the exercises. The suffix can contain a maximum of 4 characters (numbers and letters). The screenshots in this document have been taken with the suffix1234
and systemD20
. Your system id will beTRL
.
Please note that the ADT dialogs and views may change in the future due to software updates.
Follow the instructions below.
First, create the Interface CDS view ZI_RAP_Travel_####
(where ####
is your chosen suffix) for the Travel entity.
- Right-click on your travel table
ZRAP_ATRAV_####
and choose New Data Definition from the context menu.
-
Maintain
ZI_RAP_Travel_####
as name and a meaningful description (e.g. Travel BO view) in the creation wizard and choose Next to continue.
The project, the package and the referenced object have been automatically assigned.Please note
In this course, we will follow the naming convention of the Virtual Data Model (VDM) of the SAP S/4HANA where the name of interface or BO views begins with<namespace>I
(e.gZI_
) and the name of consumption or projection views begins with the namespace followed by<namespace>C
(e.g.ZC_
). -
Assign a transport request and choose Next.
-
Choose the Define View entity template from the list of Data Definition templates provided for your convenience and then choose Finish.
Excursus:
One of the main differences between CDS DDIC-based view and CDS view entity, is that the latter does not have an associated SQL view, and the name of the Data Definition object in the Project Explorer and the name of the CDS entity specified after the keywordDEFINE VIEW ENTITY
are identical. This makes the lifecycle of CDS View entities way easier.The new data definition appears in the appropriate editor.
Short Explanation:
The travel tableZRAP_ATRAV_1234
is used as data source and the table fields have been automatically inserted in the projection list between the curly brackets{}
– except the client field which is handled implicitly by the platform because the view is client specific. The view key element is specified with the keywordkey
. -
Replace the code of the travel data definition in the editor with the code snippet provided below and replace all occurrences of
####
with your chosen suffix. You can make use of the Replace All feature (Ctrl+F) in ADT for the purpose.Save
the changes, but DO NOT activate the travel interface view yet.
@AccessControl.authorizationCheck: #CHECK @EndUserText.label: 'Travel BO view' define view entity ZI_RAP_Travel_#### as select from zrap_atrav_#### as Travel association [0..*] to ZI_RAP_Booking_#### as _Booking on $projection.TravelUUID = _Booking.TravelUUID association [0..1] to /DMO/I_Agency as _Agency on $projection.AgencyID = _Agency.AgencyID association [0..1] to /DMO/I_Customer as _Customer on $projection.CustomerID = _Customer.CustomerID association [0..1] to I_Currency as _Currency on $projection.CurrencyCode = _Currency.Currency { key travel_uuid as TravelUUID, travel_id as TravelID, agency_id as AgencyID, customer_id as CustomerID, begin_date as BeginDate, end_date as EndDate, @Semantics.amount.currencyCode: 'CurrencyCode' booking_fee as BookingFee, @Semantics.amount.currencyCode: 'CurrencyCode' total_price as TotalPrice, currency_code as CurrencyCode, description as Description, overall_status as TravelStatus, @Semantics.user.createdBy: true created_by as CreatedBy, @Semantics.systemDateTime.createdAt: true created_at as CreatedAt, @Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, @Semantics.systemDateTime.lastChangedAt: true last_changed_at as LastChangedAt, @Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Booking, _Agency, _Customer, _Currency }
The travel data definition should look as follows.
Short explanation: What has changed?
- Following view annotations, specified at the top of the data definition template, have been removed, because they are not relevant for our scenario:
@AbapCatalog.viewEnhancementCategory: [#NONE] @Metadata.ignorePropagatedAnnotations: true @ObjectModel.usageType:{ serviceQuality: #X, sizeCategory: #S, dataClass: #MIXED }
- The alias
Travel
is now specified for the data source using the keywordas
. - Associations are defined for the entities Booking (
_Booking
), Agency (_Agency
), Customer (_Customer
) and Currency (_Currency
) – and exposed in the projection list. An error is currently displayed by the association_Booking
, because the specified Booking BO viewZI_RAP_Booking_####
does not yet exist. You will create it in the next step. - A CamelCase alias has been specified for each view elements using the keyword
as
.
PS: The aliasTravelStatus
is used for the view elementOverall_Status
. - To ensure uniform data processing on the consumer side,
@Semantics
annotations are used to enrich some of the fields – i.e. the currency fields and the administrative fields in the present scenario.
The currency key fieldCurrencyCode
is specified as the reference field of the currency fieldsBookingFee
andTotalPrice
using the annotation@Semantics.amount.currencyCode
.
The annotation of the administrative data is a preparation step for week 3, where the transactional behavior of the Travel List Report app will be enabled. These annotations are required to allow the automatic update of the fields on every operation.
- Following view annotations, specified at the top of the data definition template, have been removed, because they are not relevant for our scenario:
Note: DO NOT yet activate the CDS data definition as you are not yet done with the definition of the Travel interface view. The BO structure is not yet defined. This will be done in step 4.
Now, you will create the Interface CDS view ZI_RAP_Booking_####
(where #### is your chosen suffix) for the Booking entity.
-
Right-click on your booking table
ZRAP_ABOOK_####
and choose New Data Definition from the context menu. -
Maintain
ZI_RAP_Booking_####
as name and a meaningful description (e.g. Booking BO view) in the creation wizard and choose Next to continue.
The project, the package and the referenced object have been automatically assigned. -
Assign a transport request and choose Next.
-
Choose the Define View entity template from the list and then choose Finish. The new data definition appears in the editor. You can make use of the Source Code Formatter (SHIFT+1) to format the source code.
The booking data definition should look as follows.
-
Replace the booking data definition in the editor with the code snippet provided below and replace all occurrences of
####
with your chosen suffix. You can make use of the Replace All feature (Ctrl+F) in ADT for the purpose.Save the changes, but DO NOT activate the booking interface view yet.
@AccessControl.authorizationCheck: #CHECK @EndUserText.label: 'Booking BO view' define view entity ZI_RAP_Booking_#### as select from zrap_abook_#### as Booking association [1..1] to ZI_RAP_Travel_#### as _Travel on $projection.TravelUUID = _Travel.TravelUUID association [1..1] to /DMO/I_Customer as _Customer on $projection.CustomerID = _Customer.CustomerID association [1..1] to /DMO/I_Carrier as _Carrier on $projection.CarrierID = _Carrier.AirlineID association [1..1] to /DMO/I_Connection as _Connection on $projection.CarrierID = _Connection.AirlineID and $projection.ConnectionID = _Connection.ConnectionID association [1..1] to /DMO/I_Flight as _Flight on $projection.CarrierID = _Flight.AirlineID and $projection.ConnectionID = _Flight.ConnectionID and $projection.FlightDate = _Flight.FlightDate association [0..1] to I_Currency as _Currency on $projection.CurrencyCode = _Currency.Currency { key booking_uuid as BookingUUID, travel_uuid as TravelUUID, booking_id as BookingID, booking_date as BookingDate, customer_id as CustomerID, carrier_id as CarrierID, connection_id as ConnectionID, flight_date as FlightDate, @Semantics.amount.currencyCode: 'CurrencyCode' flight_price as FlightPrice, currency_code as CurrencyCode, @Semantics.user.createdBy: true created_by as CreatedBy, @Semantics.user.lastChangedBy: true last_changed_by as LastChangedBy, @Semantics.systemDateTime.localInstanceLastChangedAt: true local_last_changed_at as LocalLastChangedAt, /* associations */ _Travel, _Customer, _Carrier, _Connection, _Flight, _Currency }
The booking data definition should look as follows.
The view columns have now aliases, the associations to the entities Travel, Customer, Carrier, Flight and Currency are specified – and added to the projection list. @Semantics annotations are also specified for the currency field XXXX and the administrative fields.
Note: You are not yet through with the definition of the Booking interface view, because the BO structure is not yet defined. You will do it in step 4 of this exercise.
To avoid an error during the activation, both new CDS views – i.e. the Travel interface view and the Booking interface view – must be activated together for the first time.
-
Choose Activate All
or use the shortcut Ctrl+Shift+F3.
Select both CDS viewsZI_RAP_TRAVEL_####
andZI_RAP_BOOKING_####
on the appearing dialog, and choose Activate.Both CDS views are now activated, but warnings are displayed in the Problems view and in the editor. The reason behind this is that the authority check is allowed for these CDS views (
@AccessControl.authorizationCheck: #CHECK
), but no CDS access control is currently defined for them. You will work on this in unit 7 of week 2 where you will define a basic CDS access control. -
To run the Data Preview, choose the relevant CDS data definition – i.e.
ZI_RAP_TRAVEL_####
orZI_RAP_BOOKING_####
where####
is your chosen suffix – in the Project Explorer (or open it in the editor) and press F8.The Data Preview will open in the editor area.
Finally, you will now enhance the CDS data model to specify the business object structure: The composition model (or composition tree).
As already mentioned in the introduction of this unit, this step is not mandatory when building read-only application. It is only a preparation for the hands-on exercises of week 3 where you’re are going to enable the transactional behavior of your Travel App. Nevertheless, this is a typical step when defining your CDS data model since read-only and transactional apps can be built on top of the same data model.
Our composition model consists of two nodes: The root node travel and its child node booking.
-
Open the CDS data definition
ZI_RAP_TRAVEL_####
(where####
is your chosen suffix) and add the keywordroot
in thedefine view entity
statement to change it as follows:define root view entity ZI_RAP_Travel_####
-
Adjust the definition of the
_Booking
association to a composition which is needed to define the relationship from a parent node (travel) to a child node (booking). For that, replace the definition of the association_Booking
with the following composition definition:composition [0..*] of ZI_RAP_Booking_#### as _Booking
-
Save
the changes, but DO NOT yet activate the changes. The travel data definition as shown on the screenshot below
-
Now open the CDS data definition
ZI_RAP_BOOKING_####
and replace the definition of the association_Travel
with the definition provided below to specify the relationship from the child node (booking) to its parent node (travel).association to parent ZI_RAP_Travel_#### as _Travel on $projection.TravelUUID = _Travel.TravelUUID
-
Save
the changes and activate BOTH CDS views together by choosing Activate All
or using the shortcut Ctrl+Shift+F3, selecting both CDS views in the appearing dialog and choosing Activate.
These changes have no impact on the data. You can again run (F8) the Data Preview.
You have completed the exercise!
In this unit, you have learned
- About the Core Data Services (CDS) data model
- How to define the CDS data model and the business object's composition tree
Find the source code of the created CDS data definitions (interface views) in the /week2/sources folder:
Do not forget to replace all the occurrences of ####
with your chosen suffix in the copied source code.