Hello syambabu.allu
Great Blog on Deep Insert.Thank you for sharing this. is that possible to create Multi-level deep insert using above approach. Thank you, |
entity CompanyEntity {
. . .
linkToContact : Association to ContactEntity;
}
entity ContactEntity {
. . .
linkToCommunication : Association to CommunicationEntity;
}
entity CommunicationEntity {
. . .
linkToComment : Association to CommentEntity;
}
entity CommentEntity {
. . .
message : String;
}
{
"companyId": 1,
. . .
"linkToContact":
{
"contactId": 11,
. . .
"linkToCommunication":
{
"communicationId": 111,
. . .
"linkToComment":
{
"commentId": 1111,
. . .
keyMap.put("linkToCommunication", Collections.singletonList("communicationId"));
keyMap.put("linkToComment", Collections.singletonList("commentId"))
@Create(entity = "CompanyEntity", serviceName = "RelationService")
public CreateResponse createCompanyDeep(CreateRequest createRequest, ExtensionHelper extensionHelper) throws DatasourceException{
keyMap.put("CompanyEntity", Collections.singletonList("companyId"));
keyMap.put("linkToContact", Collections.singletonList("contactId"));
keyMap.put("linkToCommunication", Collections.singletonList("communicationId"));
keyMap.put("linkToComment", Collections.singletonList("commentId"));
HTTP Verb |
POST |
URL |
https://p123trial-acc-space-deepinsertmulti-srv.../odata/v2/RelationService/CompanyEntity |
Headers |
Content-Type: application/json |
Request body |
{ "companyId": 1, "companyName": "PetShop", "linkToContact_contactId": 11, "linkToContact": { "contactId": 11, "contactName": "Petty", "contactPhone": 123456, "linkToCommunication_communicationId": 111, "linkToCommunication": { "communicationId": 111, "socialMedia": "Facebook", "webAddress": "www.facebook.com", "linkToComment_commentId": 1111, "linkToComment": { "commentId": 1111, "message": "very nice" } } } } |
Cheers,
Carlos
namespace com.relation;
entity CompanyEntity {
key companyId : Integer;
companyName : String;
linkToContact : Association to ContactEntity;
}
entity ContactEntity {
key contactId : Integer;
contactName : String;
contactPhone : Integer;
linkToCommunication : Association to CommunicationEntity;
}
entity CommunicationEntity {
key communicationId : Integer;
socialMedia : String;
webAddress : String;
linkToComment : Association to CommentEntity;
}
entity CommentEntity {
key commentId : Integer;
message : String;
}
using com.relation from '../db/data-model';
service RelationService {
entity CompanyEntity as projection on relation.CompanyEntity
entity ContactEntity as projection on relation.ContactEntity;
entity CommunicationEntity as projection on relation.CommunicationEntity;
entity CommentEntity as projection on relation.CommentEntity;
}
package com.example;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sap.cloud.sdk.service.prov.api.EntityData;
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 {
@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();
// 2) create map for each entity of deep insert: map key field to entity / resp. map key field to association
Map<String, List<String>> keyMap = new HashMap<String, List<String>>();
// Parent entity: CompanyEntity. Here, the key field is "companyId"
keyMap.put("CompanyEntity", Collections.singletonList("companyId"));
// For nested entity: assign the key field (contactId) of navigation target entity (ContactEntity) to the navigationProperty name (linkToContact)
keyMap.put("linkToContact", Collections.singletonList("contactId"));
// multi-level deep insert: continue navigation from ContactEntity to CommunicationEntity
keyMap.put("linkToCommunication", Collections.singletonList("communicationId"));
// multi-level deep insert: continue navigation from CommunicationEntity to CommentEntity
keyMap.put("linkToComment", Collections.singletonList("commentId"));
// 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);
return CreateResponse.setSuccess().setData(result).response();
}
}
_schema-version: 2.0.0
ID: DeepInsertMulti
version: 1.0.0
modules:
- name: DeepInsertMulti-db
type: hdb
path: db
parameters:
memory: 256M
disk-quota: 256M
requires:
- name: DeepInsertMulti-db-hdi-container
- name: DeepInsertMulti-srv
type: java
path: srv
parameters:
memory: 990M
provides:
- name: srv_api
properties:
url: ${default-url}
requires:
- name: DeepInsertMulti-db-hdi-container
properties:
JBP_CONFIG_RESOURCE_CONFIGURATION: '[tomcat/webapps/ROOT/META-INF/context.xml:
{"service_name_for_DefaultDB" : "~{hdi-container-name}"}]'
- name: DeepInsertMulti-uaa
resources:
- name: DeepInsertMulti-db-hdi-container
type: com.sap.xs.hdi-container
properties:
hdi-container-name: ${service-name}
- name: DeepInsertMulti-uaa
type: org.cloudfoundry.managed-service
parameters:
service-plan: application
service: xsuaa
config:
xsappname: DeepInsertMulti-${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 |
---|---|
35 | |
25 | |
14 | |
13 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 |