最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SAPUI5 OData - How to create new entry with association to existing entity? - Stack Overflow

programmeradmin4浏览0评论

I am currently using SAPUI5/OpenUI5 to consume and modify OData Services.

I want to create a new product entry over an HTTP POST Request and have problems to properly config the associations to a category. For developing reasons I am using a reference OData Service with this metadata. The Product already has the NavigationProperty to the right Category EntrySet.

<NavigationProperty Name="Category" Relationship="ODataDemo.Product_Category_Category_Products" FromRole="Product_Category" ToRole="Category_Products"/>

I am using the following JavaScript code in my controller:

var oCategory = oModel.getData("/Categories(0)");
var oEntry = {};
oEntry.ID = "10";
oEntry.Name = "Beer";
oEntry.Category = oCategory;

oModel.create("/Products", oEntry, {
    method: "POST",
    success: function(data) {...},
    error: function(response) {...}
});

The product is successfully created /Products(10) but the relation to the existing category /Products(10)/Category is not working properly. Instead a new category with the same ID and information is created (is this meant with 'deep insert'?) but I want to use the elected category (of course).

Do I have to reference the category differently or can I create the associations manually somehow? Shouldn't the OData Service check if the category ID already exists and then use the existing entry?

Is there any best practices for such cases?

I am currently using SAPUI5/OpenUI5 to consume and modify OData Services.

I want to create a new product entry over an HTTP POST Request and have problems to properly config the associations to a category. For developing reasons I am using a reference OData Service with this metadata. The Product already has the NavigationProperty to the right Category EntrySet.

<NavigationProperty Name="Category" Relationship="ODataDemo.Product_Category_Category_Products" FromRole="Product_Category" ToRole="Category_Products"/>

I am using the following JavaScript code in my controller:

var oCategory = oModel.getData("/Categories(0)");
var oEntry = {};
oEntry.ID = "10";
oEntry.Name = "Beer";
oEntry.Category = oCategory;

oModel.create("/Products", oEntry, {
    method: "POST",
    success: function(data) {...},
    error: function(response) {...}
});

The product is successfully created /Products(10) but the relation to the existing category /Products(10)/Category is not working properly. Instead a new category with the same ID and information is created (is this meant with 'deep insert'?) but I want to use the elected category (of course).

Do I have to reference the category differently or can I create the associations manually somehow? Shouldn't the OData Service check if the category ID already exists and then use the existing entry?

Is there any best practices for such cases?

Share Improve this question edited Jan 7, 2021 at 21:46 Sandra Rossi 13.8k6 gold badges25 silver badges56 bronze badges asked Mar 9, 2017 at 14:52 Kennedy KolmesKennedy Kolmes 331 gold badge1 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It's important to note that you are using an OData V2 service. Yes, by building the request the way you are doing it, you are actually doing a deep insert.

If you think about it, it makes sense, because you would not need to send the whole category information to just link the new product to the exiting category. What if you would change something in the category data? Should a deep insert result in an update?

In any case, OData v2 has something called "links" (see the OData terminology - www.odata). Basically each "association" between entities is represented through such a link. You can manage these links separately from the entity (e.g. you can remove and create links between existing entities; without having to change the entity itself - see the OData v2 operations, chapters 2.9 to 2.12).

Depending on the data format that you are using (by default, JSON if you are using sap.ui.model.odata.v2.ODataModel), you can create entity links in the same time when creating new entities. Check out this answer: https://stackoverflow./a/4695387/7612556.

In a nutshell, you would have to write something along the lines of:

oModel.create("/Products", {
    ID: "10",
    Name: "Beer",
    Category: {__metadata: {uri: "/Categories(0)"}}
}, {
    method: "POST",
    success: function(data) {...},
    error: function(response) {...}
});
发布评论

评论列表(0)

  1. 暂无评论