java - 在有关系的实体上发布关于 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
要执行 DeepInset,您可以使用以下有效负载
{
"version": "1",
"contactNumber" : "",
"LocationDetails": {
"id":"",
"version":"",
"CityMunicipalityDetails":{
// all attributes of city class
},
"ProvinceDetails":{
// all attributes of province class
}
}
}
或者,您也可以查看$batch
OData 2 中的实现,因为深度插入是 OData 实现的更高版本的一部分,但 Olingo 2.0 JPA 处理器也在 2.0 版中实现。
PS:请检查导航属性名称,如果此解决方案不能解决问题$metadata
,请尽可能发布内容。$metadata
推荐阅读
- python - Python 3.6 及更早版本的精确时间(以纳秒为单位)?
- dart - 左对齐可滚动标签
- api - Xtream代码2.9.2如何使用Apis查看行?
- laravel-5.7 - 如何从具有多个用户数据的数据库中仅获取特定用户的数据 laravel
- python - 使用 lower() 进行不区分大小写的字典检查
- automated-tests - Botium 部分 convos 实际上并没有将对话发送到聊天机器人
- python - 如何在 Python 中返回整个字符串并从中提取一列?
- regex - Firefox 给出 SyntaxError: invalid regexp group
- angular - Ionic 3 open modal 出现未在入口组件中声明但已存在的错误
- c# - 如何在 C# 中的 ListView 上运行 Linq 查询?