BTP CAP Part 5: Domain Modeling with Entities, Associations, Compositions and Aspects
Model the domain well in CDS: entities with keys (cuid), reusable aspects (managed, temporal), types and code lists, associations (references) vs compositions (contained children with deep insert/delete), and modeling patterns that keep services and UIs simple.
Explanation
📌 KEY POINTS
✅ Use aspects from @sap/cds/common: cuid (UUID key), managed (created/modified fields), Currency, Country, CodeList.
✅ Associations reference independent entities (e.g. Order → Customer).
✅ Compositions model contained children (e.g. Order → Items) with deep insert, update and cascade delete.
✅ Keep the domain model independent of any single UI or consumer.
⚠️ Modeling independent entities (like Address shared by many) as compositions causes unexpected cascade deletes.
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Part 4 covered SaaS. Now the model itself.
━━━━━━━━━━━━━━━━━━━━
▶ 1. ENTITIES & KEYS
- cuid, natural keys where needed.
━━━━━━━━━━━━━━━━━━━━
▶ 2. ASPECTS
- managed, temporal, custom aspects.
━━━━━━━━━━━━━━━━━━━━
▶ 3. ASSOCIATIONS
- To-one, to-many with backlink (on).
━━━━━━━━━━━━━━━━━━━━
▶ 4. COMPOSITIONS
- Contained children; deep operations.
━━━━━━━━━━━━━━━━━━━━
▶ 5. PATTERNS
- Code lists, localized texts, reuse.
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Model Orders with composition of Items.
- Associate Orders to Customers.
- Test deep insert via OData.
✅ CHECKPOINT: deleting an order deletes its items but not the customer.
Code example
===== db/schema.cds =====using { cuid, managed, Currency, sap.common.CodeList } from '@sap/cds/common';entity Orders : cuid, managed { customer : Association to Customers; items : Composition of many OrderItems on items.parent = $self; currency : Currency; status : Association to OrderStatus;}entity OrderItems : cuid { parent : Association to Orders; product : String; qty : Integer; }entity Customers : cuid { name : String; }entity OrderStatus : CodeList { key code : String(1); }Real project scenario
An application modeled Address as a composition of Customer and of Order; deleting an order deleted addresses still used by customers. Remodeling Address as an associated entity fixed the data loss.
Common mistakes
- Compositions for shared entities.
- No aspects.
- UI-driven modeling.
- Late renames.
- Missing backlinks.
Best practices
- Composition only for contained data.
- Use common aspects.
- Consumer-independent model.
- Stable names.
- Proper on-conditions.
Interview angle
Questions: "Association vs composition?", "What are aspects?", "What is deep insert?"
Strong answer: keys/aspects, associations, compositions, patterns, lifecycle implications.
Practice this lesson
Quick check
1. What is the key distinguishing behavior of a 'composition' relationship compared to an 'association' in a CDS data model?
2. Deleting an order also deleted addresses still used by customers. What modeling error caused this?
3. Which aspect from @sap/cds/common adds created/modified timestamps and users?
Interview questions
1. An order-management CAP model causes accidental data loss and complex services. How do you improve the domain model?
Answer it in your own words first, then compare.
2. What are the most important design decisions, trade-offs, and best practices in CAP?
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- BTP CAP Part 1: What CAP Is and Why It Matters on SAP BTP
- BTP CAP Part 2: Building and Exposing Services with CDS
- BTP CAP Part 3: Securing CAP Services with XSUAA Role-Based Authorization
- BTP CAP Part 4: Multitenancy, Extensibility and Production Deployment
- BTP CAP Part 6: Service Handlers — Before, On, After and Transactions
- BTP CAP Part 7: OData Services, Actions, Functions and Draft