Search code examples
odatasapui5hana

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


in my SAPUI5-Application I got the following function that takes the data and creates an entry in my HANA DB:

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();
}

The creating process works and my entries get saved. In the next step I wanted to implement my table in HANA in that way, that the ID of the entries will be autoincremented so the user does not have to enter it. I used the following command to create my table:

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

That worked, table is created. The problem now is, if I use the code above without providing the ID, the following error is logged by the console:

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

The entry does not get saved in my DB. If I execute the following SQL-Statements in my HANA Workbench without providing the ID, it works:

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

So I did some searching on google but did not find a proper solution on how to do this. My goal is that the entry gets saved and the ID is provided by my HANA DB without providing it from my SAPUI5 Application.


Solution

  • Probably you are using ODataV2 XSODATA implementation which does not support auto-increment. The possible solution here is to use a database sequence and then with a separate request get a value from it and use it in OData create statement.