在spring boot olingo上做一篇关于一个有关系的实体的文章



我有一些由spring-boot和olingo构建的odata V2 api。我正在尝试向一个与有关系的实体发帖

public class CompanyProfileDetail {
private Long id;
@NotNull
private Long version;
@NotNull
@OneToOne
@JoinColumn(name = "company_profile_id", referencedColumnName = "id")
private CompanyProfile companyProfile;
...
}
public class CompanyProfile {
private Long id;

@NotNull
private Long version;
...
}

因此,当我尝试使用CompanyProfileDetails api 发布这篇文章时

{
"Version": "1",
"CompanyProfile": "1"
}

它不起作用,但这个

{
"Version": "1",
"CompanyProfileDetails" : {
"__metadata": {
"id": "http://localhost:8080/odata/CompanyProfiles(1L)",
"uri": "http://localhost:8080/odata/CompanyProfiles(1L)",
"type": "default.CompanyProfile"
}
}
}

工作。所以现在我想知道这是不是一个odata发布关系的标准,或者是否有一些设置可以接受第一个结构。

仅供参考;CompanyProfileDetails;第二个数据结构是olingo如何自动命名相关实体。它实际上是CompanyProfiles实体

另一个例子:

public class CompanyProfile {
private Long id;

@NotNull
private Long version;
@ManyToOne
@JoinColumn(name = "address", referencedColumnName = "id")
private Location address;
@Column(name="contact_number")
private String contactNumber;
@NotNull
@Column(name="name")
private String name;
@NotNull
@Column(name="status")
private String status;
...
}
public class Location {

@Id
@Column(name = "id")
@NotNull
private Long id;

private Long version;
@ManyToOne
@JoinColumn(name = "city", referencedColumnName = "id")
private CityMunicipality city;
@ManyToOne
@JoinColumn(name = "province", referencedColumnName = "id")
private Province province;
}

工作请求如下:

{
"Version": "1",
"ContactNumber": "1245",
"Name": "OIL Tycoon corporation",
"Status": "OK",
"LocationDetails" : {
"__metadata": {
"id": "http://localhost:8080/odata/Locations(2L)",
"uri": "http://localhost:8080/odata/Locations(2L)",
"type": "default.Location"
}
}
}

但是这不起作用:

{
"Version": "1",
"ContactNumber": "1245",
"Name": "OIL Tycoon corporation",
"Status": "OK",
"Address" : "2",
}

在这种情况下,存在Id为2的位置。

应要求提供$元数据。这是第二个例子。希望这能有所帮助!

<EntityType Name="CompanyProfile">
<Key>
<PropertyRef Name="Id"></PropertyRef>
</Key>
<Property Name="Address" Type="Edm.Int64" Nullable="true"></Property>
<Property Name="ContactNumber" Type="Edm.String" Nullable="true" MaxLength="255"></Property>
<Property Name="Id" Type="Edm.Int64" Nullable="false"></Property>
<Property Name="Version" Type="Edm.Int64"></Property>
<Property Name="Name" Type="Edm.String"></Property>
<NavigationProperty Name="LocationDetails" Relationship="default.CompanyProfile_Location_Many_ZeroToOne0" FromRole="CompanyProfile" ToRole="Location"></NavigationProperty>
</EntityType>
<EntityType Name="Location">
<Key>
<PropertyRef Name="Id"></PropertyRef>
</Key>
<Property Name="City" Type="Edm.String" Nullable="true"></Property>
<Property Name="Id" Type="Edm.Int64" Nullable="false"></Property>
<Property Name="Province" Type="Edm.String" Nullable="true"></Property>
<Property Name="Region" Type="Edm.Region" Nullable="true"></Property>
<Property Name="Version" Type="Edm.Int64" Nullable="true"></Property>
</EntityType> 

Olingo 2.0 JPA处理器的默认实现以<Navigation Property Name>Details格式命名导航属性,因此例如,如果您有一个名为Location的联接,则导航属性将命名为LocationDetails

要执行深度插入,您可以使用以下有效载荷

{
"version": "1",
"contactNumber" : "",
"LocationDetails": {
"id":"",
"version":"",
"CityMunicipalityDetails":{
// all attributes of city class
},
"ProvinceDetails":{
// all attributes of province class
}

}
}

或者,您也可以查看OData 2中$batch的实现,因为深度插入是OData实现的后续版本的一部分,但Olingo 2.0 JPA处理器也在2.0版本中实现。

PS:请检查$metadata中的导航属性名称,如果此解决方案不能解决问题,请尽可能发布$metadata内容。

您指定了两个不起作用的post请求。对于第一个,下面的请求主体应该可以工作。

{
"version": "1",
"companyProfile": {
"id" : 1,
"version" : "1"

}
}

对于第二个,

{
"version": "1",
"contactNumber" : "",
"address": {
"id":"",
"version":"",
"city":{
// all attributes of city class
},
"province":{
// all attributes of province class
}

}
}

相关内容

最新更新