Association in CDS Views
In CDS Views (Core Data Services) within SAP, an association is a way of defining a relationship between two entities (tables, views, or other entities) without directly joining them. It allows you to link related data in a flexible, object-oriented way.Unlike joins, which physically combine the data at the database level, associations define a logical link between entities in a CDS view. The actual data retrieval happens when the association is explicitly accessed, making it more flexible and efficient in some cases.
Key Features of Associations:
➯ No Immediate Data Retrieval: An association does not immediately pull in the related data when the CDS view is executed. The data is only retrieved when it’s specifically needed (e.g., in an ABAP SELECT query or in a consumption layer like SAP Fiori).
➯ Lazy Loading(OnDemand Join): The related data from the associated table or view is not fetched until it is required. This can lead to better performance in certain scenarios.
➯ Semantic Relationship: Associations are designed to represent a relationship between entities in a more semantic and intuitive way (e.g., “one-to-many” or “one-to-one” relationships).
Here’s an example of how an association is defined in a CDS view:
➯ Lazy Loading(OnDemand Join): The related data from the associated table or view is not fetched until it is required. This can lead to better performance in certain scenarios.
➯ Semantic Relationship: Associations are designed to represent a relationship between entities in a more semantic and intuitive way (e.g., “one-to-many” or “one-to-one” relationships).
Here’s an example of how an association is defined in a CDS view:
@AbapCatalog.sqlViewName: 'ZASSOC_EX1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Association example for CDS'
@Metadata.ignorePropagatedAnnotations: true
define view ZABAP_ASSOC
as select from /dmo/travel as travel
association [0..1] to /DMO/I_Agency as _Agency on $projection.AgencyId = _Agency.AgencyID
association [0..1] to /DMO/I_Customer as _Customer on $projection.CustomerId = _Customer.CustomerID
{
key travel_id as TravelId,
agency_id as AgencyId,
customer_id as CustomerId,
begin_date as BeginDate,
end_date as EndDate,
_Agency,
_Customer
}
Cardinality in Associations:
➯ [0..1] : Zero or one related entry (optional relationship).
➯ [1..1] : Exactly one related entry (mandatory relationship).
➯ [0..n] : Zero or more related entries (optional, multiple entries allowed).
➯ [1..n] : One or more related entries (mandatory, multiple entries allowed).
➯ [1..1] : Exactly one related entry (mandatory relationship).
➯ [0..n] : Zero or more related entries (optional, multiple entries allowed).
➯ [1..n] : One or more related entries (mandatory, multiple entries allowed).
How to access Association data.
Run your CDS to see the initial list of data. Right click on anyone row → Follow Association
Run your CDS to see the initial list of data. Right click on anyone row → Follow Association
Hooray!!! We have successfully wrote code for Association in CDS View. Do check out more blog post on ABAP on HANA topic. Happy Learning.