odatasapui5sap-web-idemockserverweb-ide

How to read an aggregated entity from Odata Service SAP ui5


In OData Model V2, I'm struggling by reading the odata of my mocked server:

I want to get access to the child entity which is associated with a Parent entity.

From Open_Station I can navigate to_Order_Header:

[{
    "results": [
        {
            "__metadata": {
                "id": "http://WebService/Open_Station(Station='CHE1',Task='5246')",
                "uri": "http://WebService/Open_Station(Station='CHE1',Task='5246')",
                "type": "cds_zui_check_dialog.Open_StationType"
            },

            "Station": "CHE1",
            "Task": "5246",
            "to_Order_Header": {
                "__deferred": {
                    "uri": "http://WebService/Open_Station(Station='CHE1',Task='5246')/to_Order_Header"
                }
            }
        }
    ]
}

]

And from to_Order_Header, I can navigate to Order_Item or Stock:

[{
"__metadata" : {
  "id" : "http://WebService/Order_Header(OrderNumber='00000000100000000853',Station='CHE1',OrderIndicator='')",
  "uri" : "http://WebService/Order_Header(OrderNumber='00000000100000000853',Station='CHE1',OrderIndicator='')",
  "type" : "cds_zui_check_dialog.Order_HeaderType"
},
"OrderNumber" : "00000000100000000853",
"Station" : "CHE1",
"OrderIndicator" : "",
"to_Order_Items" : {
  "__deferred" : {
    "uri" : "http://WebService/Order_Header(OrderNumber='00000000100000000853',Station='CHE1',OrderIndicator='')/to_Order_Items"
  }
},
"to_Stock" : {
  "__deferred" : {
    "uri" : "http://WebService/Order_Header(OrderNumber='00000000100000000853',Station='CHE1',OrderIndicator='')/to_Stock"
  }

} ]

In my controller, I have a metho ReadOdata read data like this:

ReadOdata: function()
{
   var oModel = new sap.ui.model.odata.v2.ODataModel("http://WebService");
   return new Promise(function(resolve, reject)
   {
       oModel.read("Open_Station(Station='CHE1',Task='5246')/to_Order_Header", {
           
           success : function (data) {
              resolve(data);
           },
           error: function (oError) {
               reject(oError);
           }
       });
   });
   
},

I always get a failure like this:

Request failed with status code 404: GET Open_Station(Station='CHE1',Task='5246')/to_Order_Header 
- [{"code":404,"message":"Resource not found","persistent":false,"targets":["/Open_Station(Station='CHE1',Task='5246')/to_Order_Header"],"type":"Error"}]
 sap.ui.model.odata.ODataMessageParser

I think the way I want to read the associated entity is wrong. Please help me.


Solution

  • I found a solution. It was a failure in the annotation of the Path:

    Instead of writing:

     oModel.read("**to_Open_Station**(Station='CHE1',Task='5246')/to_Order_Header", {
    

    I was writing:

    oModel.read("*Open_Station*(Station='CHE1',Task='5246')/to_Order_Header", {
    

    In the MetaData, the NavigationProperty was called to_Open_Station.