Skip to Content

oData multilevel entity with self reference?

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


association.jpg (72.0 kB)
Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    Posted on Oct 18, 2018 at 04:21 PM

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


    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 :-)

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.