cancel
Showing results for 
Search instead for 
Did you mean: 

One-to-One relationship between two objects of the same Entity type

Former Member
0 Kudos

I am facing some troubles with NW Gateway about a deep insert for one one-to-one relationship:

This is my JSON:

{

  FirstName : "Juan",

  LastName : "Duran"

  Spouse : {

    FirstName : "Maria",

    LastName : "Duque"

  }

}

And I want those two objects to be inserted in a "ZPEOPLE" table. How should I define and do that? (NW Gateway).

I must use the ODataModel.create("/People",...,...,...,...) method.

A 'Person' entity type should be defined as PersonId(PK), FirstName, LastName, SpouseId (The one-to-one relationship between two 'Person' entities).

Thank you in advance.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Juan

In order to create a relationship, you can POST to a navigation endpoint from your primary entity.

If you have a navigation such as '.../Persons(1)/Spouse', this will return a "spouse flavour" person entity if one is defined.

To create a spouse, simply POST to the same URL with a Person entry in the body. i.e. rather than create a deep insert, post the person, then post the spouse. You can $batch these two requests of a LUW is required (LUW is implied by a deep insert). The key point is that the first 'person' already exists and can provide some context for the creation of the second person.

In terms of how you realise the backend, it will depend on what you actually need the service to do - you may need to do a bit of debugging in the called methods to refine your logic, e.g. in CREATE_ENTITY, determine if it is person A (canonical URI) or person B (navigated URI) being created, this may affect the logic.

In OData terms this is a neater way to do it than a deep insert and probably more compliant to the OData URI schemes.

Regards

Ron.

Former Member
0 Kudos

But that must be transactional, something like 'no person can be stored in the DB without an spouse', that's why I cannot batch the operation, I have to receive both entities and perform the store in SAP, if not, then I just don't save none of them.

former_member184867
Active Contributor
0 Kudos

Are you facing any specific problem, or you are stuck at the model...

Modelling would be like this...

In Service Builder you need to define the following Entity, Association and Navigation Property

1. Entity : E1( You may give your entity name )

2. Association : You need to create an association which will be used by the Navigation property

3. Navigation Property : In step 2 when , when toy create association you can also create the Navigation property. if you did not do it in this step you need to create it separately using the association create in step 2. Once created the navigation property will be visible under service builder and will look like

You need to implement CREATE_DEEP_ENTITY in the DPC EXT class, where you write the logic for database insert.

The JSON payload will look like...

{

"PERSONID" :1,

"NAME" :"XXXX",

"SPOUSEID" : 2,

"SPOUSE" :{      //<------This is the Navigation Property Name

"PERSONID" :2,

"NAME" :"YYYY",

"SPOUSEID" : 1}

}

Former Member
0 Kudos

Hello Atanu, thank you for your response.

We have done that before, mapping an association 1-1 in the modeller, but when we send the payload, it says:

Property 'SpouseID' is invalid.

You know? That Spouse relationship is an example, our real scenario is this:

Entities:

RelationshipType (Id, Name)

BusinessPartner(Id, Name)

And we want to associate two Business Partners with an specific RelationshipType, so the PK of that new entity (Relationship) would be (BusinessPartner1Id, BusinessPartner2Id, RelationshipTypeId).

Now, when the BusinessPartners aren't created yet, we want to do the following:

{

  Relationship : {

  RelationshipTypeId : "REL001",

    BusinessPartner1 : {

      Name : "Juan",

      Country : "VE"

    },

    BusinessPartner2 : {

      Name : "Dario",

      Country : "DO"

    }

  }

}

So the relationship is created and two new BPs are deep inserted, another example would be this (case one BP is existant):

{

  Relationship : {

    RelationshipTypeId : "REL001",

    BusinessPartner1Id : "06000001",

    BusinessPartner2 : {

      Name : "Dario",

      Country : "DO"

    }

  }

}

So that is the thing is causing my NW Gateway partner to not to move forward.

We also (once created the Relationship entity), would like to do something like this:

/Relationship(BusinessPartner1Id='06000001',BusinessPartner2Id='06000002',RelationshipTypeId='REL001')/BusinessPartner2, and have the proper navigation.

Thank you!

AshwinDutt
Active Contributor
0 Kudos

Hello Juan,

Please have a look at the below blog and maintain the cardinality as per your requirement.

With this you should be able to do it.

Regards,

Ashwin