SAPUI5 OData-如何创建与现有实体关联的新条目? [英] SAPUI5 OData - How to create new entry with association to existing entity?

查看:235
本文介绍了SAPUI5 OData-如何创建与现有实体关联的新条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用SAPUI5/OpenUI5来使用和修改OData服务.

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

我想通过HTTP POST请求创建一个新的产品条目,并且在正确配置与某个类别的关联时遇到问题.出于开发原因,我正在使用参考 OData服务元数据的a>.该产品已经具有正确的Category EntrySet的NavigationProperty.

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"/>

我在控制器中使用以下JavaScript代码:

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) {...}
});

已成功创建产品/Products(10),但与现有类别/Products(10)/Category的关系无法正常工作.而是创建了一个具有相同ID和信息的新类别(这是否意味着深度插入"?),但是我想使用选择的类别(当然).

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

我是否必须以其他方式引用类别,或者可以以某种方式手动创建关联? OData服务不应该检查类别ID是否已经存在,然后使用现有条目吗?

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?

推荐答案

请务必注意,您正在使用OData V2服务.是的,通过按照您的方式构建请求,您实际上是在进行深层插入.

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?

在任何情况下,OData v2都有一个称为链接"的内容(请参见OData术语- www.odata.org ).基本上,实体之间的每个关联"都是通过这样的链接表示的.您可以与实体分开管理这些链接(例如,可以删除和创建现有实体之间的链接;而不必更改实体本身-请参见

In any case, OData v2 has something called "links" (see the OData terminology - www.odata.org). 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).

根据所使用的数据格式(如果使用sap.ui.model.odata.v2.ODataModel,默认情况下为JSON),可以在创建新实体的同时创建实体链接.看看这个答案: https://stackoverflow.com/a/4695387/7612556 .

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.com/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) {...}
});

这篇关于SAPUI5 OData-如何创建与现有实体关联的新条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆