Core Data Services (CDS) are central to SAP CAP development. Beyond basic entity definitions, CAP developers benefit from advanced modeling techniques such as compositions, projections, and understanding managed vs. unmanaged entities. These concepts improve code reuse, data integrity, and maintainability.
This guide explores these advanced CDS modeling patterns and shows practical examples for enterprise CAP applications.
Compositions in CDS
Compositions define strong ownership between entities. The parent entity owns the lifecycle of the child entities. Deleting the parent also deletes the children automatically.
Example: Orders and OrderItems
entity Orders {
key ID : UUID;
customerName : String;
items : Composition of many OrderItems on items.order = $self;
}
entity OrderItems {
key ID : UUID;
order : Association to Orders;
productName : String;
quantity : Integer;
}
Key Points:
Compositionindicates strong ownership- Child entity cannot exist without the parent
- Supports cascading deletes
Projections in CDS
Projections allow you to expose a subset or transformed view of an entity. They are read-only by default unless extended.
Example: Product Projection
entity Products {
key ID : UUID;
name : String;
price : Decimal(10,2);
cost : Decimal(10,2);
}
entity ProductPublic as projection on Products {
ID,
name,
price
}
Benefits:
- Control which fields are exposed in services
- Simplify API contracts
- Enhance security by hiding sensitive fields
- Enable multiple views of the same entity
Managed vs. Unmanaged Entities
CAP distinguishes between managed and unmanaged entities:
Managed Entities
- CAP automatically generates standard CRUD operations
- CDS annotations like
@cds.persistence.existsor@odata.draft.enabledapply - Suitable for service-based access
Example:
entity Customers {
key ID : UUID;
name : String;
}
CAP automatically creates:
READ,CREATE,UPDATE,DELETEhandlers- Persistence in the underlying database
Unmanaged Entities
- CAP does not automatically generate handlers or persistence
- Used for projections, external services, or calculated data
- Developers must implement handlers manually
Example:
entity CustomerSummary @(unmanaged) {
ID : UUID;
name : String;
orderCount : Integer;
}
Use Cases:
- Aggregated views
- Joined or computed entities
- External data sources
Combining Compositions and Projections
You can combine compositions and projections for flexible APIs.
Example: Exposing Order Summary
entity OrderSummary as projection on Orders {
ID,
customerName,
items {
productName,
quantity
}
}
Notes:
- Nested compositions are preserved
- Projections can hide fields or sensitive data
- Supports OData
$expandautomatically
Best Practices for Complex CDS Modeling
- Use compositions for strong parent-child relationships
- Use associations for weak relationships
- Use projections to expose only necessary fields
- Keep unmanaged entities for derived data or analytics
- Avoid circular compositions
- Document entities for team clarity
- Use CDS annotations for behavior (drafts, readonly, restrict)
Real-World Example
- Order Management System:
- Orders (parent) composed of OrderItems (children)
- Projections for public APIs expose only order summary
- Managed entities for database persistence
- Unmanaged entities for reporting and analytics
- E-Commerce App:
- Products and Categories with associations
- ProductPublic projection hides internal cost
- Calculated discount views as unmanaged entities
Conclusion
Advanced CDS modeling in CAP through compositions, projections, and proper use of managed vs. unmanaged entities enables scalable, maintainable, and secure applications. Mastering these concepts ensures clean data modeling, optimized APIs, and flexibility for enterprise scenarios.
you may be interested in this blog here:-
Don’t Fear the Update: Navigating the Challenges of how to implement sap note
Five Top Technology Investment Drivers for 2024
How many dollars worth of RSU does Salesforce typically offer an MTS (experienced hire) on joining?
Integration cloud system to HANA Cloud Platform using Cloud Connector

WhatsApp us