mysqlspring-bootjpaolingo

Olingo/JPA does not store foreign keys in batch requests


I made an odata service with olingo2, jpa and spring-boot based on this GitHub repository.

I upgraded the version of olingo to 2.0.11 and spring boot to 2.3.0 as I explained here.

I tried to create an object with the following batch request payload:

--batch_661f-eaa8-27e8
Content-Type: multipart/mixed; boundary=changeset_cd7f-245c-cede

--changeset_cd7f-245c-cede
Content-Type: application/http
Content-Transfer-Encoding: binary

POST FormSet HTTP/1.1
sap-contextid-accept: header
Accept: application/json
Accept-Language: en-US
DataServiceVersion: 2.0
MaxDataServiceVersion: 2.0
Content-Type: application/json
Content-Length: 114

{"IconId":"IC1","Deleted":0,"Name":"mm","ValidFrom":"\/Date(1591260532000)\/","ValidTo":"\/Date(1593074937000)\/"}
--changeset_cd7f-245c-cede--

--batch_661f-eaa8-27e8--

The problem is it will create the Form object in the database but it will not set the foreign key (i.e. IconId) as I passed. Here is the response:

--batch_3e32b3e0-b61d-4bb9-9c3c-4c6a22ca07a4
Content-Type: multipart/mixed; boundary=changeset_779fb130-eb85-40c4-be2c-114c210bbda0

--changeset_779fb130-eb85-40c4-be2c-114c210bbda0
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 201 Created
DataServiceVersion: 2.0
Location: http://localhost:9090/odata.svc/FormSet(0)
Content-Type: application/json
Content-Length: 522

{"d":{"__metadata":{"id":"http://localhost:9090/odata.svc/FormSet(0)","uri":"http://localhost:9090/odata.svc/FormSet(0)","type":"me.cimply.Form"},"Deleted":0,"IconId":null,"Id":0,"Name":"mm","ValidFrom":"\/Date(1591260532000)\/","ValidTo":"\/Date(1593074937000)\/","Icon":{"__deferred":{"uri":"http://localhost:9090/odata.svc/FormSet(0)/Icon"}},"Questions":{"__deferred":{"uri":"http://localhost:9090/odata.svc/FormSet(0)/Questions"}},"Surveys":{"__deferred":{"uri":"http://localhost:9090/odata.svc/FormSet(0)/Surveys"}}}}
--changeset_779fb130-eb85-40c4-be2c-114c210bbda0--
--batch_3e32b3e0-b61d-4bb9-9c3c-4c6a22ca07a4--

As it can be seen the IconId in the response is null.

Here is the JPA auto generated entity for Form:

package me.cimply.ask.odata.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Set;


/**
 * The persistent class for the forms database table.
 * 
 */
@Entity
@Table(name="forms")
@NamedQuery(name="Form.findAll", query="SELECT f FROM Form f")
public class Form implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false)
    private byte deleted;

    @Column(nullable=false, length=255)
    private String name;

    @Column(name="valid_from", nullable=false)
    private Timestamp validFrom;

    @Column(name="valid_to")
    private Timestamp validTo;

    //bi-directional many-to-one association to Icon
    @ManyToOne
    @JoinColumn(name="icon_id")
    private Icon icon;

    public Form() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public byte getDeleted() {
        return this.deleted;
    }

    public void setDeleted(byte deleted) {
        this.deleted = deleted;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Timestamp getValidFrom() {
        return this.validFrom;
    }

    public void setValidFrom(Timestamp validFrom) {
        this.validFrom = validFrom;
    }

    public Timestamp getValidTo() {
        return this.validTo;
    }

    public void setValidTo(Timestamp validTo) {
        this.validTo = validTo;
    }

    public Icon getIcon() {
        return this.icon;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
    }

    //....
}

The other problem is the Id which is auto increment while zero returned that I know how to resolve it by setting the id generator strategy. But finding a way for setting the foreign key is important!


Solution

  • I used the same approach as explained here and solved the problem. We need to customize the batch processor functions for POST and PUT functions.