ERPClimb logoERPClimb
CDS domain modeling
SAP Cloud Application Programming Modelbeginner 5 min read

CDS domain modeling — SAP Guided Tutorial

Free sample lesson · open to everyone

Key takeaway

Modeling entities, keys, associations and compositions in CDS: when a child belongs to its parent (composition) versus merely references it (association), and how reuse aspects like cuid and managed keep the model consistent.

In this lessonShow sections

Visual overview

CDS domain modeling — learning sheetFlow: Entities + keys: cuid → Association: reference → Composition: owned children → Aspects: managed, reuse. Key ideas: ENTITIES AND KEYS; ASSOCIATIONS; COMPOSITIONS; TYPES, ASPECTS AND ANNOTATIONS. Watch out: Modeling items as a plain association or a string key loses deep insert, cascade delete and draft handling.ERPClimb.com / CAP / LESSON 2/12 / LEARNING SHEETCDS domain modeling1 · HOW IT FLOWS1Entities +keyscuid2Associationreference3Compositionowned children4Aspectsmanaged, reuse2 · KEY IDEAS EXPLAINEDEntities And Keys`entity Books : cuid, managed { title :String(111); stock : Integer; price :Decimal(9,2); currency : Currency; }``using { cuid, managed, Currency } from'@sap/cds/common';` gives standard keys, auditfields and code lists.AssociationsTo-one managed: `author : Association to Authors;`— CAP adds author_ID.To-many: `books : Association to many Books onbooks.author = $self;` — a backlink, no extracolumn.Compositions`entity Orders : cuid { items : Composition ofmany { key pos : Integer; book : Association toBooks; quantity : Integer; } }` — an inline…Deep insert: POST an order with items in onerequest; CAP writes both tables in onetransaction.Types, Aspects And AnnotationsDefine reusable types (`type Amount :Decimal(15,2);`) and aspects for repeated fieldgroups.Validation annotations such as `@mandatory`,`@assert.range`, `@assert.target` give declarativeinput checks.3 · REMEMBER · WATCH OUTRememberEntities are defined in db/*.cds inside a namespace; `cuid` gives aUUID key, `managed` adds createdAt/createdBy/modifiedAt/modifiedBy.Association = a reference to something with its own lifecycle (anorder references a customer).Composition = a part-of relationship; children are created, read anddeleted with the parent (order → order items).!Watch outModeling items as a plainassociation or a string key losesdeep insert, cascade delete anddraft handling.4 · IN A REAL PROJECTA purchase request app stored items in a separate entity with `request_ID : String`. Deleting a request leftorphan items, draft editing of items failed, and the Fiori object page could not create items inline.Remodelling items as `Composition of many` fixed deep insert, cascade delete and draft in one change.Original ERPClimb learning sheet • Verify against your SAP release and edition

The situation: a real project scenario

A purchase request app stored items in a separate entity with request_ID : String. Deleting a request left orphan items, draft editing of items failed, and the Fiori object page could not create items inline. Remodelling items as Composition of many fixed deep insert, cascade delete and draft in one change.

Explanation

Everything later in CAP — services, handlers, authorization — builds on the domain model defined in db/*.cds, so getting the modeling right here pays off throughout the project.

Entities live inside a namespace, and the reuse aspects from @sap/cds/common save you from reinventing basics: using { cuid, managed, Currency } from '@sap/cds/common'; gives you cuid for a UUID primary key and managed for automatic createdAt/createdBy/modifiedAt/modifiedBy fields.

UUID keys via cuid are the right default for transactional data; natural keys make sense only where the business genuinely owns them, such as ISO currency codes, and even then they carry the risk that a "natural" key sometimes turns out not to be stable — a customer number that later gets renumbered is a classic example.

Associations and compositions both model relationships, but they mean different things. An association, like author : Association to Authors, is a reference to something with its own independent lifecycle — CAP generates the foreign key column (author_ID) for you, and associations let you write path expressions in queries, such as SELECT title, author.name FROM Books.

A backlink association, books : Association to many Books on books.author = $self, works the same way without adding a column. A composition, by contrast, models a true part-of relationship: child records are created, read, and deleted together with the parent.

An inline composition like items : Composition of many { key pos : Integer; book : Association to Books; quantity : Integer; } on an Orders entity means you can deep-insert an order with its items in a single request, deleting the order cascades to delete its items, and draft editing works across the whole composition tree.

The rule of thumb is simple: if a child record makes no sense without its parent, model it as a composition, not an association — modeling line items as a plain association or a free-text foreign key loses deep insert, cascade delete, and draft handling all at once.

Beyond entities and relationships, reusable types (type Amount : Decimal(15,2);) and aspects let you avoid repeating field groups, and declarative validation annotations such as @mandatory, @assert.range, and @assert.target give you input checks without writing handler code. It's also worth keeping UI-specific annotations and labels out of db/ entirely — they belong in app/ or srv/ annotation files, so the domain model stays a clean, UI-agnostic description of the business data.

To practice: model an Orders entity with an inline composition of Items and an association to Customers; POST one order with two items and confirm both tables are populated; delete the order and confirm the items disappear while the customer record remains; then run cds compile db --to sql and read the generated foreign keys. For every relationship in your model, you should be able to say why it's an association or a composition, and exactly what happens to the child record on delete.

Code example

ABAP Code
namespace my.orders;using { cuid, managed, Currency } from '@sap/cds/common'; entity Customers : cuid { name : String(80); } entity Orders : cuid, managed {  customer : Association to Customers @assert.target;  currency : Currency;  items    : Composition of many {    key pos  : Integer;    product  : String(40);    quantity : Integer @assert.range: [1, 9999];    price    : Decimal(9,2);  };}

Watch out

Common mistakes

  • Using Association to many Items for line items, so deleting an order leaves orphan items.
  • Storing foreign keys as free String fields instead of managed associations.
  • Putting UI annotations and labels into the db layer.
  • Using natural business keys that later need to change (e.g. a customer number that gets renumbered).

Do this

Best practices

  • Use cuid and managed from @sap/cds/common for transactional entities.
  • Model part-of relationships as compositions so deep insert, cascade delete and draft work.
  • Use @assert.target on associations where referential integrity must be checked on write.

In the interview

Interview angle

Expect "association vs composition" in almost every CAP interview. Give the lifecycle rule, a concrete example (order items vs customer), and the runtime consequences: deep insert, cascade delete and draft only work across compositions.

Practice this lesson

Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.

Quick check

0 of 4 questions attempted

Question 1 of 4

Order items should be created, read and deleted together with their order. How should they be modeled?

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

1Explain the difference between an association and a composition in CDS, with an example.

2How do you design keys for a CAP domain model, and when would you not use UUIDs?

Was this lesson helpful?

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.