cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a parent child relationship

sumant_purohit
Advisor
Advisor
0 Kudos

Hi ,

I want to create an odata web service which would output a json which has attribute children.

Children can again have children.

E.g :

{

           "chainid":1,

            "docuid":1,

            "backrefid":0,

            "docnr":"4500005376",

            "doctyp":"",

            "name":"PurchaseOrder",

            "username":"KRICK",

            "docdate":"\/Date(1391040000000)\/",

            "doctime":"PT00H00M00S",

            "docyear":"0000",

            "node":"",

            "colorcode":"",

            "tabname":"EKKO",

            "awkey":"",

            "awtyp":"",

            "bukrs":"0001",

            "image":"images/email_64.png",

             "children": [

             {

              "chainid":1,

            "docuid":2,

            "backrefid":1,

            "docnr":"5000004089",

            "doctyp":"",

            "name":"MM GoodsMvnt",

            "username":"KRICK",

            "docdate":"\/Date(1391040000000)\/",

            "doctime":"PT10H44M35S",

            "docyear":"2014",

            "node":"",

            "colorcode":"G",

            "tabname":"MKPF",

            "awkey":"",

            "awtyp":"MKPF",

            "bukrs":"",

            "image":"images/email_64.png",

            } ] }

Any idea on how to implement this using entity set or otherwise?

Thanks in advance.

Regards,

Sumant

Accepted Solutions (1)

Accepted Solutions (1)

former_member184867
Active Contributor
0 Kudos

Hello Sumant,

This would be possible using the Navigation property. Here are some steps to achieve the same

Step 1: Create ParentEntity and the ChildEntity.

Step 2: Establish  an association between these two entities with cardinality 1:N

Step 3:Create a navigation from ParentEntity to ChildEntity using this association. Assume the Navigation   property name is 'children'

Step 4: Implement the DPC logic to get the data.

Step 5: If you fire the URL http://host:port/namespace/servicename/ParentEntitySet?$expand=children & $format=json  you get the desired data..

Regards,

Atanu

sumant_purohit
Advisor
Advisor
0 Kudos

Hello Atanu,

Thanks for your answer.

But my childentity can also have children.

So how do I create this relationship?

Establishing a parent to child 1:N would mean 1 parent with have N children.

E.g :

root:{

   name: "root",

   description: "root description",

    child: {

               name: "item1",

              description: "item1 description",

               child: {

                           name: "subitem1-1",

                           description: "subitem1-1 description",

                           child: {

                           name: "subsubitem1-1-1",

                           description: "subsubitem1-1-1 description",

                           }

                        }

             }

   }

Thanks & Regards,

Sumant

former_member184867
Active Contributor
0 Kudos

It is possible to drill down to children of children as well.For this the URL would look like

http://host:port/namespace/servicename/ParentEntitySet?$expand=<Navigation property name from ppaarreentt to children1>/<Navigation property name from children1 to children1' children>

Example at

http://services.odata.org/V3/OData/OData.svc/Categories?$expand=Products/Supplier

sumant_purohit
Advisor
Advisor
0 Kudos

Hello Atanu,

So I will have to create another association between the 2 child entities and a navigation property from

1 child entity to another child entity with 1 : N?

Thanks & Regards,

Sumant


former_member184867
Active Contributor
0 Kudos

yes

Answers (2)

Answers (2)

Former Member
0 Kudos

The main problem here is the terminology and way of thinking.

In OData there is no real parent/child hierarchy, this is being imposed by the way of thinking. This is a navigation and, as such, is not restricted to 'downwards' movement.

If you think in navigation terms, this becomes a recursion exercise.

Here is an analogy where the entities are of the same type: rail station network.

The "principal" entity is a Station. It has a navigation to an entityset called NextStations; this set contains any stations that are directly connected by a rail link.

A Station can be a terminus or a way station.

With this model you can move from Station to Station. You can make a one way trip or a two way trip. The trip can be linear from any via any to any, or it could be circular from origin via any to origin.

Once you move from one station to another, the state changes and a new navigation can be undertaken. The 'data rules' for every station are the same, it doesn't matter how far station foo is from station bar

The model is simple yet useful. There is no need to define a whole network of stations in some complex model. A client consuming a service can overlay their own context on this - you could for example describe the London Tube system in this way, and a UI could come up with the well known map of this system. 

Regards

Ron.

sumant_purohit
Advisor
Advisor
0 Kudos

Currently I have taken two entities one as parent another child which have the same structure.


Every node which has a child is a parent.

A navigation property from parent to child with 1 : N.

Another navigation property from child to child 1 : N.

This seems to give me a json which is close to what is required in the UI though not exact.

But it looks like I have to work with that in the javascript.

Thanks all for your help.

Regards,

Sumant

kammaje_cis
Active Contributor
0 Kudos

Another approach:

As I understand you have parent and child of the same type. So I would suggest to create only one entity but associate with itself and create navigation to the same entity again (1:N). So that you can achieve deep children functionality.

sumant_purohit
Advisor
Advisor
0 Kudos

Hi Krishna,

Yes both the parent and child are of the same type.

But while creating the association type i need to reference the parentname of one entity to the name of another.

How do I write this association?

e.g : if I create

begin of item,

   name type string,

   description type string,

    parentname type string,

enf of item.

and create an association  like this for 1 : N cardinality  ?

  lo_association = model->create_association(
                                iv_association_name = 'item_items'
                                iv_left_type = 'item'
                                iv_right_type = 'item'
                                iv_right_card = cardinality_feed
                                iv_left_card = cardinality_entity ).

Regards,

Sumant

AshwinDutt
Active Contributor
0 Kudos

Hello Sumant,

Define the below way.


lo_association = model->create_association(

                                iv_association_name = ' <Asoociation_Name> '

                                iv_left_type = '<Parent_Entity>'

                                iv_right_type = '<Child_Enityt>'

                                iv_right_card = 'N'

                                iv_left_card = '1' ).


Regards,

Ashwin

sumant_purohit
Advisor
Advisor
0 Kudos

Hello Ashwin,

I want to make both the parent and the child as the same entity.

  lo_association = model->create_association(
                                iv_association_name = 'item_items'
                                iv_left_type = 'item'
                                iv_right_type = 'item'
                                iv_right_card = 'N'
                                iv_left_card = '1' ).

Regards,

Sumant

AshwinDutt
Active Contributor
0 Kudos

Hello Sumant,

Code is fine.

But i have never tried creating association between the same entities as you are trying now ( 1 to N ) and i do not know how GW supports that, so i cannot comment on behavior of the GW

Kindly fire the service to see metadata and see how GW responds to that. If any problem we can see later.

If at all there is any problem in model level ( not syntax errors ), you will be getting error ' Model Contains Error ' when GW tries to load the metadata once u add service in /iwfnd/maint_service.

Regards,

Ashwin

sumant_purohit
Advisor
Advisor
0 Kudos

Hello Ashwin,

      lo_association = model->create_association(
                                iv_association_name = 'item_items'
                                iv_left_type = 'item'
                                iv_right_type = 'item'
                                iv_right_card = 'N'

                               iv_left_card = '1').
lo_entity_type = model->get_entity_type( iv_entity_name = 'item' ).

    lo_entity_type->create_navigation_property(
                      iv_property_name = 'children'
                      iv_association_name = 'item_items' ).

It throws an error "Resource not found for the segment children'.

Regards,

Sumant


AshwinDutt
Active Contributor
0 Kudos

Hello Sumant,

The code which u have written ( below code ) has to be changed.

You cannot create navigation with  lo_entity_type referring  to /iwbep/if_mgw_odata_entity_typ.

lo_entity_type->create_navigation_property(

                      iv_property_name = 'children'

                      iv_association_name = 'item_items' ).


Code will be like this :


lo_nav_property referring  to /iwbep/if_mgw_odata_nav_prop.  


lo_nav_property = lo_entity_type->create_navigation_property( iv_property_name  = 'children'

                                                           iv_association_name = 'item_items' ).

Please make changes.

Regards,

Ashwin

kammaje_cis
Active Contributor
0 Kudos

Sumant,

Why don't you data model in Service Builder?. That way you do not have to handle through code.