Association An association is a declarative relationship between CDS entities (like foreign keys), defined using the association keyword. Key Characteristics: Lazy/Evaluated-on-demand: Data from the association is not fetched unless it is explicitly accessed using a path expression (like to_customer.name). Navigation-style access: You can access associated data using dot (.) notation. Defined once, reused many times: Associations can be defined once and used in multiple views or queries. Cardinality can be defined: e.g. [0..1], [1..*].
define view ZSalesOrder as select from sales_order
association [0..1] to customer as _Customer on $projection.customer_id = _Customer.id
{
key sales_order_id,
customer_id,
_Customer.name
}
Here, _Customer is an association. It won’t trigger a join unless _Customer.name is accessed.
define view ZSalesOrderJoin as select from sales_order
inner join customer on sales_order.customer_id = customer.id
{
sales_order.id,
customer.name
}
Here, a real-time INNER JOIN is performed.
When to Use What?
Use Association:
- When modeling data relationships.
- When you want flexibility and better performance for optional fields.
- When the associated data isn’t always needed.
Use Join:
- When you always need data from both sources.
- When performing aggregations or filters based on joined fields.
- When doing complex multi-joins with specific types (e.g. inner join).