In this blog, we will cover some use cases and OData exposure of CDS Views created with Key column. You can find our other blogs on CDS here.
Code Push Down in ABAP applications is enabled by CDS Views, which are an extension of the ABAP Dictionary. ABAP CDS provides a framework for defining and consuming semantically rich data models on the central database of the application server, AS ABAP. ABAP Dictionary manages the Data Definition Language (DDL) and the Data Control Language (DCL) based on which the data models are defined. CDS Views can be created with or without a key column. Let’s look at some use cases for views created with a key column.
Use Cases for Creating View with Key
There are various scenarios where a view needs to be created with Key columns.
1. Master Data
Aggregating the results across multiple dimensions requires an Analytical Model. A basic view is created on the Master data from the DDIC Tables/Views. As it is fetching the data from database table there should be no data redundancies as the view is created with the Key Column specified.
@ObjectModel.dataCategory: #DIMENSION is the annotation that is used to represent the Master data.
2. Foreign Key Association
Relationships between entities are defined by Associations. The Source and Target entities relate to each other with the help of the Foreign Key. The Primary Key of the Source entity (Representative Key) acts as the Foreign Key for the Target entity.
3. Value Help
Value Help provides the possible correct values of an input field to the user. This Value Help can be established with the Foreign Key Association in CDS.
Hierarchy in CDS can be created on the view with dataCategoryas DIMENSION. The ‘Key’ field is mandatory for specifying the Hierarchy.
OData Exposure of View with Key
OData (Open Data Protocol) is the best way of consuming RESTful APIs. ABAP CDS view can be exposed as OData Service in different ways. In our scenario, OData Exposure is done using the Annotation @OData.publish: true.
CDS views can be exposed as OData regardless of whether a key column is present in the view or not. In this blog, we will cover the different methods of accessing the exposed data for a CDS view with a key column.
- Accessing the entire data using the Navigation Property, and
- Accessing Individual Records
Creating the CDS View with Key Column and its OData Exposure
For this Illustration, the CDS view is created by consuming BSEG Table with three input parameters. The Key is set and OData service is enabled.
Input Parameters : P_CurDate , P_ACC, P_Cur
OData is generated and the metadata contains two EntityTypes that are created, one for the Key and Parameters and the other EntityTypefor the other columns.
The first EntityTypecreated contains the Key column BELNR and the three Input Parameters.
Accessing the Exposed Data
As seen in the image below, Setis the Name of the NavigationProperty for the CDS View.
Hence the entire dataset can be accessed using the OData Service given below
Accessing individual records
Individual records can be accessed by passing values for the Input Parameters & the Key as shown below:
We have now seen two distinct methods of accessing data exposed through OData when the view contains a Key column.
Learn more about Visual BI’s SAP HANA offerings here.