cloud-cap-samples icon indicating copy to clipboard operation
cloud-cap-samples copied to clipboard

Add multiple books as one JSON batch request

Open patricebender opened this issue 2 years ago • 7 comments

For me it was not obvious how I could POST multiple books as part of one request. I got hinted to the OData specification where I was able to find some information on this. I would suggest we add a sample $batch request to our prominent bookshop sample, so that it is easier for beginners to understand this concept.

patricebender avatar Oct 08 '21 10:10 patricebender

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 08 '21 10:10 CLAassistant

Thanks @GitP4tt @hm23 can you check if this is proper JSON batch? @beckermarc I guess this is not supported yet on Java, right? Then we should add a comment hint in the example.

chgeo avatar Oct 08 '21 11:10 chgeo

I guess this is not supported yet on Java, right? Then we should add a comment hint in the example

No, Java only supports multipart batch requests. It doesn't support the JSON format.

beckermarc avatar Oct 08 '21 11:10 beckermarc

I guess this is not supported yet on Java, right? Then we should add a comment hint in the example

No, Java only supports multipart batch requests. It doesn't support the JSON format.

I can look into it and try to refactor the JSON batch into a multipart batch request. That should work for both stacks, shouldn't it?

patricebender avatar Oct 11 '21 08:10 patricebender

Should add: it's pretty poor that OData doesn't support creating multiple rows by one request other than through $batch requests. CAP does btw:

INSERT.into (Books) .entries ([/* ... → all of same kind */])
srv.create (Books) .entries ([/* ... → all of same kind */])

These requests can be sent as single operations and delegated to databases in single prepared statements. In contrast to that OData $batches can contain arbitrary mixes of queries → no guaranteed way to optimise.

danjoa avatar Oct 11 '21 09:10 danjoa

One more comment: as also discussed in other forums, a better way to do bulks inserts in OData would actually be through actions accepting arrays of entities or deep inserts of fake entities. E.g. modelled in CDS:

service Sue {
   entity Foo {...} // OData does not support bulk inserts
   entity Dummy { foos: Composition of many Foo } // Deep insert would allow bulk insert
   action AddFoos (foos : many Foo); // Action for bulk insert
}

Both need custom coding though

danjoa avatar Oct 11 '21 09:10 danjoa

Hey Heiko @hm23 :)

Could you have a look at the JSON batch? I've of course tested it, but let me know if this can be improved.

patricebender avatar Oct 15 '21 12:10 patricebender