Sub topic 2
CDS View Entities and Associations as the Building Block
This chapter assumes you know basic SQL (SELECT, JOIN, GROUP BY) and the difference between transactional and analytical workloads. It also assumes you know what a business object such as a customer or sales item is. You do not need prior ABAP CDS experience.
What a CDS view entity is
A CDS (Core Data Services) view entity is a database view defined in ABAP Development Tools in Eclipse. You write it in the CDS data definition language. The system creates the SQL view on the SAP HANA database when you activate it, so the logic runs in the database and not in ABAP.
Every analytical object in embedded analytics is a CDS view entity: dimensions, cubes and queries. They differ only in the annotations they carry. Learning the plain view entity first makes the later chapters easier.
A view entity has three parts:
- Annotations, which start with
@and describe the view. They set the label, the authorization check and the analytical role. - The
define view entitystatement with aselect fromclause naming a data source. - An element list in braces, which names the fields that the view exposes.
Here is a small view entity over a hypothetical custom table zcustomer:
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer'
@Analytics.dataCategory: #DIMENSION
define view entity ZI_CustomerDim
as select from zcustomer
{
key customer_id as CustomerId,
@Semantics.text: true
customer_name as CustomerName,
country as Country
}The key keyword marks the primary key. The as keyword gives each field a CDS name, so the table column customer_id is exposed as CustomerId. Consumers see the CDS names only.
The @Analytics.dataCategory: #DIMENSION annotation is the one analytical line here. Later chapters explain it. For now, read the rest as ordinary CDS.
Associations
An association is a named, reusable join definition. It states how one view relates to another, but it does not execute a join by itself. The join runs only when a query uses the association.
Compare this with an SQL JOIN, which always executes. With an association, a consumer that never reads the related fields pays nothing for the relationship.
The next view entity defines an association to the customer dimension:
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales item cube'
@Analytics.dataCategory: #CUBE
define view entity ZI_SalesItemCube
as select from zsales_item
association [0..1] to ZI_CustomerDim as _Customer
on $projection.CustomerId = _Customer.CustomerId
{
key item_uuid as ItemUuid,
@ObjectModel.foreignKey.association: '_Customer'
customer_id as CustomerId,
@Semantics.currencyCode: true
currency as Currency,
@DefaultAggregation: #SUM
@Semantics.amount.currencyCode: 'Currency'
net_amount as NetAmount,
_Customer
}Read the association line piece by piece:
| Part | Meaning |
|---|---|
| `association [0..1]` | Cardinality: each sales item has zero or one matching customer. |
| `to ZI_CustomerDim` | The target view entity. |
| `as _Customer` | The association name. By convention it starts with an underscore. |
| `on $projection.CustomerId = _Customer.CustomerId` | The join condition. `$projection` refers to a field in this view's own element list. |
Listing _Customer in the element list exposes the association. Consumers can then reach _Customer.CustomerName through a path expression, without writing a join. The @ObjectModel.foreignKey.association annotation tells analytical tools that CustomerId is a foreign key to this association.
How an association becomes a join
The diagram shows what happens when a query reads a field through the association.
The generated join is a left outer join. Sales items without a customer stay in the result, with empty customer fields.
Common errors
- Forgetting to expose the association. If
_Customeris not in the element list, consumers cannot use it. - Using a wrong cardinality. Declaring
[0..1]when a target can return several rows per source row duplicates rows and inflates sums. Check that the target key is fully covered by theoncondition. - Reading the CDS name, not the column name, in the
oncondition. Use$projection.CustomerId, notcustomer_id, when you refer to an element of the current view. - Setting
@AccessControl.authorizationCheck: #NOT_REQUIREDon views that hold sensitive data. It is acceptable for a customer dimension of non-sensitive master data in this example, but cubes usually use#CHECK.
Practice
Exercise
A query selects NetAmount from ZI_SalesItemCube and never touches _Customer. Is the join to ZI_CustomerDim executed? Why?
Show answer
No. An association defines a join but does not run it. The join is added only when a query reads a field through a path expression such as _Customer.CustomerName.
Exercise
You add association [0..1] to ZI_CustomerDim as _Customer on $projection.CustomerId = _Customer.CustomerId to a view but do not list _Customer in the element list. What can consumers not do?
Show answer
They cannot navigate to the association, so they cannot reach customer fields through _Customer. Listing _Customer in the element list exposes it.