How to query result count with SAP Cloud SDK java in a batch

131 views Asked by At

I am using the OData v2 Type-safe Client API to get a list of entities with a filter. The filter list and the entity count is quite long, so I need to use a batch request and paging. If I do not use batch then the url gets truncated, so that is a must.

I need to know the count of the whole result set.

I tried to use FluentHelperRead<>.count(), but I could not put that into the batch, as it resulted a FluentHelperCount and batch.addReadOperations() does not support that.

I also tried to .withQueryParameter("inlinecount", "allpages") in a batch, but I could not extract the resulting odata.count from the batch result.

Is there any way to do this and get a count of all result entities with batch and filtering and paging?

1

There are 1 answers

1
Christoph Schubert On

You are right, currently the Typed OData API of the SAP Cloud SDK does not allow to add a count operation to a batch request.

However, it is possible to do this with a workaround:

DefaultBusinessPartnerService service = new DefaultBusinessPartnerService();
ODataRequestCount countRequest = service.getAllBusinessPartner().filter(BusinessPartner.FIRST_NAME.startsWith("A")).count().toRequest();
ODataRequestBatch batchRequest = service.batch().toRequest();
// you can add any other request to the batch body here as well
batchRequest.addRead(countRequest);
Long countOfEntries = batchRequest.execute(client).getResult(countRequest).as(Long.class);

Here you are "escaping" from the typed API into the generic OData client. In there it is possible to pass on the count operation, potentially together with other operations, if you want.

In parallel we will have a deeper look into this and maybe consider this as a feature request for a future version.