cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

oData multilevel entity with self reference?

_Dimitri_
Product and Topic Expert
Product and Topic Expert
0 Likes
2,481

Hi Experts,

I have a flat list of products which are in basicly a product hierarchy. So each entry in the table has an id and parent id, while the parent id can point to an id of a different entry in the table. What i now want to achieve is to build a oData which can expand the whole tree while the level of deepenes is unknown and respond with a nested json that represents the hierarchy.

So i have built the oData with one entity (product list) and have defined association from the entity to it self by mapping id to parent id with an 1:M association. I also have created a Navigation Property which points to the association.

Now i started to overwrite the GET_EXPANDED_ENTITYSET method. What i want to achieve is, that whenever the oData is called with the url /sap/opu/odata/sap/ZORG_TEST_SRV/PRODAREASet?$expand=PRODAREAS&$format=json it responds with a nested json that shows the complete hierarchy, all levels.

Now i am wondering how to build the hierachy inside the method, beause the generated structure in mpc class does not have any table for child nodes.

i tried to built a local type (product), a table type of my local type and the local type that includes the table of itself but this seem not to work. I have also tried a class instead of structure but then copy_data_to_ref call inside GET_EXPANDED_ENTITYSET gave me an error that ref are not allowed.

Is this use case even possible with oData if you do not know how deep your structure can be? Basicly i need an "expand all" with unknown deepness for an entity that self references 1:n


Thank you and Best Regards


Dimitri

Accepted Solutions (1)

Accepted Solutions (1)

RalfHandl
Product and Topic Expert
Product and Topic Expert

You have to explicitly specify how many levels you want to expand, e.g.

GET PRODAREASet?$expand=PRODAREAS,PRODAREAS/PRODAREAS,PRODAREAS/PRODAREAS/PRODAREAS

to expand three levels deep.

OData V4 adds a shortcut

GET PRODAREASet?$expand=PRODAREAS($levels=3)

and even allows

GET PRODAREASet?$expand=PRODAREAS($levels=max)

to request the maximum number of levels supported by the service (which is allowed to defend itself against DOS attacks by interpreting "max" appropriately 🙂

_Dimitri_
Product and Topic Expert
Product and Topic Expert
0 Likes

Thanks a lot for the clarification, sadly i have a v2 oData.

Answers (0)