【发布时间】:2025-12-21 17:50:15
【问题描述】:
我有一些由 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>
【问题讨论】:
-
当我们在公司简介中而不是在公司简介详细信息中时,这里的一对一映射是真正有意义的。
-
啊,这只是一个简化版本,用于显示代码中的关系是如何建立的。此外,本例中的 CompanyProfile 已经存在。我没有创建新的 CompanyProfile,我只是将其设置为 CompanyProfildeDetails。
-
不要在一个 json 中包含所有属性。具有关系的属性应该是子 json。您可以参考我的答案并尝试该格式。希望这应该工作
标签: java spring-boot odata olingo