How to implement
Deep Insert
in
SAP Cloud Application Programming Model
(Part 2: using UUID)
Quicklinks:
Sample Project files
Part 1: Intro
Part 3: Multi-Level
Part 4: Consume Remote Service
Part 5: Remote Service with SDK
key companyId : UUID;namespace com.relation;
entity CompanyEntity {
key companyId : UUID;
companyName : String;
linkToContact : Association to ContactEntity;
}
entity ContactEntity {
key contactId : UUID;
contactName : String;
contactPhone : Integer;
}UUID contactGuid = UUID.randomUUID();
Map<String, Object> inlineContactMap = (Map<String, Object>)mapForCreation
.get("linkToContact");
inlineContactMap.put("contactId", contactGuid); mapForCreation.put("linkToContact_contactId", contactGuid);| HTTP Verb |
| POST |
| URL |
| https://...deepinsertdemo-srv.cfapps..../odata/v2/RelationService/CompanyEntity |
| Headers |
| Content-Type: application/json |
| Request body |
| { "companyName":"CarShop", "linkToContact":{ "contactName":"Bill", "contactPhone":12345678 } } |
namespace com.relation;
entity CompanyEntity {
key companyId : UUID;
companyName : String;
linkToContact : Association to ContactEntity;
}
entity ContactEntity {
key contactId : UUID;
contactName : String;
contactPhone : Integer;
}using com.relation from '../db/data-model';
service RelationService {
entity CompanyEntity as projection on relation.CompanyEntity;
entity ContactEntity as projection on relation.ContactEntity;
}
package com.example.deep;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sap.cloud.sdk.service.prov.api.DataSourceHandler;
import com.sap.cloud.sdk.service.prov.api.EntityData;
import com.sap.cloud.sdk.service.prov.api.EntityMetadata;
import com.sap.cloud.sdk.service.prov.api.ExtensionHelper;
import com.sap.cloud.sdk.service.prov.api.exception.DatasourceException;
import com.sap.cloud.sdk.service.prov.api.operations.Create;
import com.sap.cloud.sdk.service.prov.api.request.CreateRequest;
import com.sap.cloud.sdk.service.prov.api.response.CreateResponse;
public class ServiceImplementation {
private static Logger logger = LoggerFactory.getLogger(ServiceImplementation.class);
@Create(entity = "CompanyEntity", serviceName = "RelationService")
public CreateResponse createCompany(CreateRequest createRequest, ExtensionHelper extensionHelper) throws DatasourceException{
// 1) retrieve the request payload, the data to create in backend
Map<String, Object> mapForCreation = createRequest.getData().asMap();
// special handling required in case of UUID: FWK cannot generate it for inline entity
Map<String, Object> inlineContactMap = (Map<String, Object>)mapForCreation.get("linkToContact");
// check if request is deep insert
if(inlineContactMap != null) {
// manually generate Guid for inline-entity-key-field (Contacts) and foreign-key-field (Companies)
UUID contactGuid = UUID.randomUUID();
inlineContactMap.put("contactId", contactGuid); // fill the key field of inline entity (Contacts)
mapForCreation.put("linkToContact_contactId", contactGuid);// fill the forein-key field of "Companies" entity
}
// 2) our actual task is: specify key field for navigation entity
//Compose the map of key list for all entities of the deep insert
Map<String, List<String>> keyMap = new HashMap<String, List<String>>();
// the key map for the parent entity: Companies. Here, the key field is "companyId"
keyMap.put("CompanyEntity", Collections.singletonList("companyId"));
// here we assign the key field (contactId) of navigation target entity (Contacts) to the navigationProperty name (contact)
keyMap.put("linkToContact", Collections.singletonList("contactId"));
// 3) send data to database, including the info about keys
EntityData entityDataToCreate = EntityData.createFromDeepMap(mapForCreation, keyMap, "RelationService.CompanyEntity");
// execute it in database
EntityData result = extensionHelper.getHandler().executeInsertWithAssociations(entityDataToCreate, true);// true to return created entity
return CreateResponse.setSuccess().setData(result).response();
}
}_schema-version: 2.0.0
ID: DeepInsertDemo
version: 1.0.0
modules:
- name: DeepInsertDemo-db
type: hdb
path: db
parameters:
memory: 256M
disk-quota: 256M
requires:
- name: DeepInsertDemo-db-hdi-container
- name: DeepInsertDemo-srv
type: java
path: srv
parameters:
memory: 990M
provides:
- name: srv_api
properties:
url: ${default-url}
requires:
- name: DeepInsertDemo-db-hdi-container
properties:
JBP_CONFIG_RESOURCE_CONFIGURATION: '[tomcat/webapps/ROOT/META-INF/context.xml:
{"service_name_for_DefaultDB" : "~{hdi-container-name}"}]'
- name: DeepInsertDemo-uaa
resources:
- name: DeepInsertDemo-db-hdi-container
type: com.sap.xs.hdi-container
properties:
hdi-container-name: ${service-name}
- name: DeepInsertDemo-uaa
type: org.cloudfoundry.managed-service
parameters:
service-plan: application
service: xsuaa
config:
xsappname: DeepInsertDemo-${space}
tenant-mode: dedicated
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 36 | |
| 27 | |
| 26 | |
| 26 | |
| 26 | |
| 24 | |
| 23 | |
| 22 | |
| 22 | |
| 20 |