OData V4: Use Fields from Child Entity as Filters

Think about a simple data model with a header entity(Header) and a child entity(Text) which are connected with navigations. And we want to provide fields from the child entity as possbile filters for query.
With OData V2, we need to use virtual element and implement SADL filter exit to achive this. But in V4, we don't need self-implementation anymore since it is supported in nature.  In V4 it is called Lambda Operators, includes 'any' and 'all'. Details can be found from official document: OData Version 4.0. Part 2: URL Conventions Plus Errata 03 (oasis-open.org)

The 'Text' entity has a field named 'Language'. I want to get all 'Header' entity instances which its child entity 'Text' has records with 'Language' equals to 'DE'. This a simple INNER JOIN in SQL world. In OData V4, the URL syntax should have below pattern.
https://host/odata/srv/Header? $filter=_Text/any(d:d/Language eq 'DE')&$count=true
'_Text' is the assoication name from entity 'Header' to its child entity 'Text'.

Another example for 'all':
https://host/odata/srv/Header? $filter=_Text/all(d:d/TextLength gt 10)&$count=true
With this example, only those 'Header' records, which all 'Text' records with TextLength greater than 10 will be returned.

In simple words, 'any' means at least one, and 'all' means always.

SAPUI5 V4 ODataModel already supports this, which means application developers don't need to build the URL manually. Again, SAPUI5 V4 ODataModel does all dirty works for you. It adds Lambda operators automatically if it detects any filters are from child entities.
Check SAPUI5 constants sap.ui.model.FilterOperator.all and sap.ui.model.FilterOperator.any for further information.

Comments