Now accepting guest editorial from SAP, Oracle, Workday and ServiceNow practitioners.Pitch a guide
The Sign-Off Table

Lesson 2 of 5

Defining view entities, joins and associations

10 min

Listen to this lesson

0:00 / 9:01

A CDS view entity is defined in a data definition in ADT with define view entity, a name, a data source and a projection list in curly brackets. SAP recommends view entities for new development. The older DDIC-based CDS views (define view, which generated an extra SQL view in the dictionary) are marked obsolete in current ABAP documentation and cannot be created in ABAP Cloud; you will still find them in converted code, and SAP provides a migration tool that converts them to view entities (check the documentation for your release). When you create a data definition, ADT offers a template that already contains the recommended header annotations, which the next lesson explains.

  1. Create the data definitionADT: New, Core Data Services, Data Definition; choose the view entity template
  2. Name the sourceselect from ZCMC_CORE_AGR as Agreement
  3. List the fieldskey fields first; rename to business names such as CoreReturnAgreement
  4. Add associationsto I_Customer and to the item view
  5. Add calculationsCASE, CAST, arithmetic, session variables
  6. Activate and previewData Preview in ADT; follow associations from a row
Building ZI_CMC_CoreAgreement, the basic view over the agreement table.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201

Joins and associations

You can join data sources with inner, left outer, right outer and cross joins, just as in SQL. But CDS gives you something better for most relationships: the association. SAP describes associations as forward-declared joins. You declare the relationship once, with a target, an alias that by convention starts with an underscore, an ON condition and optionally a cardinality such as of many to exact one. The database only turns it into a join when a consumer actually uses a field from the target, which SAP calls join-on-demand. Exposing the association in the projection list lets consumers navigate it from ABAP SQL or from other views through path expressions such as _Customer.CustomerName.

ElementIn ZI_CMC_CoreAgreementWhy
Data sourceTable ZCMC_CORE_AGRThe custom agreement header carried from ECC
KeyCoreReturnAgreementAgreement number, renamed from the table field
Association to customer_Customer to I_Customer, many to exact one, on CustomerEach agreement belongs to exactly one fleet customer
Association to items_Item to ZI_CMC_CoreAgreementItem, one to manyAn agreement has several products with deposits
Calculated fieldIsExpired: CASE when ValidTo is before $session.system_dateLets the app flag lapsed agreements without ABAP code

Main elements of Contoso’s basic agreement view (illustrative names).

Contoso’s basic agreement view sits between SAP’s released customer view and its own item view, linked by associations with declared cardinality.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201

Calculated fields and expressions

View entities support cast expressions, arithmetic, CASE expressions, built-in string, numeric, date and time functions, the coalesce function, literals, input parameters and session variables such as $session.system_date. Contoso's item view multiplies Quantity by DepositPerUnit to give TotalDeposit. Aggregate expressions such as SUM and COUNT with a GROUP BY clause turn the item view into a per-customer total for the deposit report. Amount fields need their currency reference, which is set through a semantics annotation, or activation will complain.

A calculated field in the item view and an aggregate on top replace the old report’s loops with two small view entities.
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201
  1. Will consumers always need the other entity’s fields?Yes → a join is acceptable · No → use an association (join on demand)
  2. Is the relationship reused across views?Yes → expose the association once in the basic view · No → keep it local
  3. Is the child existentially dependent on the parent?Yes → a composition (as in RAP business objects) · No → a plain association
  4. Do you know the cardinality?Yes → declare it, it documents the model · No → find out before you build
Join, association or composition?
The Sign-Off Table · thesignofftable.com/academy/sap-abp-201

Compositions are a special association used in RAP business objects: a to-child association from parent to child and a to-parent association back, expressing that the child cannot exist without the parent. In SAP's example, a booking always belongs to a travel and is deleted with it. Contoso's agreement items will become composition children when the team builds the RAP app in the next course.

What would you do?

In CMQ, Priya's team finds that the open deposit list shows some agreements twice. The view joins ZCMC_CORE_AGR to the customer view on Customer. What is the most likely cause and the right fix?

Key points
  • Define new CDS models with define view entity; DDIC-based views are the legacy form.
  • Associations are forward-declared joins, executed only when used (join on demand).
  • Name associations with a leading underscore and declare cardinality.
  • Use CASE, CAST, arithmetic, functions and session variables for calculated fields.
  • Compositions express existential parent-child dependency for RAP business objects.

Check your understanding · not graded

Lesson check

  1. 1. Which statement defines a CDS view entity?

  2. 2. When does SAP HANA turn an association into a join?

  3. 3. What does a composition express?

  4. 4. Which ADT feature lets you jump from a row to its associated customer?