【发布时间】:2020-09-23 05:18:39
【问题描述】:
我基于 GitHub repository 使用 olingo2、jpa 和 spring-boot 做了一个 odata 服务。
按照here的说明,我将 olingo 的版本升级到了 2.0.11,spring boot 升级到了 2.3.0。
我尝试使用以下批处理请求有效负载创建对象:
--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--
问题是它会在数据库中创建Form 对象,但它不会在我传递时设置外键(即IconId)。回复如下:
--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--
可以看出响应中的IconId 为空。
这是 JPA 自动生成的 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;
}
//....
}
另一个问题是 ID 是 auto increment 而返回零,我知道如何通过设置 id 生成器策略来解决它。但是找到设置外键的方法很重要!
【问题讨论】:
标签: mysql spring-boot jpa olingo