Lesson 2 of 5
Defining view entities, joins and associations
10 min
Listen to this lesson
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.
- Create the data definitionADT: New, Core Data Services, Data Definition; choose the view entity template
- Name the sourceselect from ZCMC_CORE_AGR as Agreement
- List the fieldskey fields first; rename to business names such as CoreReturnAgreement
- Add associationsto I_Customer and to the item view
- Add calculationsCASE, CAST, arithmetic, session variables
- Activate and previewData Preview in ADT; follow associations from a row
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.
| Element | In ZI_CMC_CoreAgreement | Why |
|---|---|---|
| Data source | Table ZCMC_CORE_AGR | The custom agreement header carried from ECC |
| Key | CoreReturnAgreement | Agreement number, renamed from the table field |
| Association to customer | _Customer to I_Customer, many to exact one, on Customer | Each agreement belongs to exactly one fleet customer |
| Association to items | _Item to ZI_CMC_CoreAgreementItem, one to many | An agreement has several products with deposits |
| Calculated field | IsExpired: CASE when ValidTo is before $session.system_date | Lets the app flag lapsed agreements without ABAP code |
Main elements of Contoso’s basic agreement view (illustrative names).
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.
- Will consumers always need the other entity’s fields?Yes → a join is acceptable · No → use an association (join on demand)
- Is the relationship reused across views?Yes → expose the association once in the basic view · No → keep it local
- Is the child existentially dependent on the parent?Yes → a composition (as in RAP business objects) · No → a plain association
- Do you know the cardinality?Yes → declare it, it documents the model · No → find out before you build
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?
- 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. Which statement defines a CDS view entity?
2. When does SAP HANA turn an association into a join?
3. What does a composition express?
4. Which ADT feature lets you jump from a row to its associated customer?