如何从 SAPUI5 应用程序处理 HANA 中的自动递增 ID?

How to handle an autoincremented ID in HANA from a SAPUI5-application?

在我的 SAPUI5 应用程序中,我得到了以下函数,它获取数据并在我的 HANA 数据库中创建一个条目:

onCreateNewCustomer: function(){
        var oEntry = {};
        oEntry.NAME = this.byId("name").getValue();
        oEntry.CITY = this.byId("city").getValue();
        oEntry.PHONE = this.byId("phone").getValue();                
        oEntry.ID = this.byId("id").getValue();

        // Post data to the server
        this.getOwnerComponent().getModel("CustomerModel").create("/Customer", oEntry, null);
        this.byId("createCustomer").close();
        //location.reload();
}

创建过程有效,我的条目已保存。在下一步中,我想以这种方式在 HANA 中实现我的 table,条目的 ID 将自动递增,因此用户不必输入它。我使用以下命令创建我的 table:

create column table TABLE
     (ID bigint not null primary key generated by default as IDENTITY,
      FIRSTNAME nvarchar(30))

成功了,table 已创建。现在的问题是,如果我在不提供 ID 的情况下使用上面的代码,控制台会记录以下错误:

The following problem occurred: HTTP request failed 400,Bad Request,The serialized resource has an missing value for member 'ID'.

条目没有保存在我的数据库中。如果我在我的 HANA Workbench 中执行以下 SQL-Statements 而没有提供 ID,它会起作用:

insert into TABLE (FIRSTNAME) values (‘David’);
insert into TABLE (FIRSTNAME) values (‘Mike’);      
insert into TABLE (FIRSTNAME) values (‘Bobby’); 

所以我在 google 上进行了一些搜索,但没有找到关于如何执行此操作的合适解决方案。我的目标是保存条目并且 ID 由我的 HANA 数据库提供,而不是从我的 SAPUI5 应用程序中提供。

您是否尝试通过注释掉以下代码来创建新记录?

oEntry.ID = this.byId("id").getValue();

对于标识字段,如果您提供值,则必须明确标识您提供的是列值。否则,只需从 INSERT 命令中省略列名和值。

可能您正在使用不支持自动递增的 ODataV2 XSODATA 实现。这里可能的解决方案是使用数据库序列,然后通过单独的请求从中获取值并在 OData create 语句中使用它。