英文:
Doing a post on spring boot olingo on an entity with a relationship
问题
我有一些基于 Spring Boot 和 Olingo 构建的 OData V2 API。我正在尝试对具有关联的实体进行发布。
当我尝试使用 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 {
// 类定义
}
public class Location {
// 类定义
}
工作的请求看起来像这样:
{
"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 的 Location 是存在的。
根据你的要求,这是第二个例子的 $metadata 内容。希望这能帮助你理解!
<EntityType Name="CompanyProfile">
<!-- 属性和导航属性的定义 -->
</EntityType>
<EntityType Name="Location">
<!-- 属性和导航属性的定义 -->
</EntityType>
英文:
I have some odata V2 apis built from spring-boot and olingo. I'm trying to a post to an entity with a relationship
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;
...
}
So when I try to post this using the CompanyProfileDetails api
{
"Version": "1",
"CompanyProfile": "1"
}
it doesn't work but this
{
"Version": "1",
"CompanyProfileDetails" : {
"__metadata": {
"id": "http://localhost:8080/odata/CompanyProfiles(1L)",
"uri": "http://localhost:8080/odata/CompanyProfiles(1L)",
"type": "default.CompanyProfile"
}
}
}
works. So now I'm wondering if this is an odata standard of posting with relationships or is there some setting that will accept the first structure.
FYI the "CompanyProfileDetails" in the second data structure is how olingo automatically names the related entity. It's actually the CompanyProfiles entity
As another example:
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;
}
The working request looks like this:
{
"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"
}
}
}
However this does not work:
{
"Version": "1",
"ContactNumber": "1245",
"Name": "OIL Tycoon corporation",
"Status": "OK",
"Address" : "2",
}
Location with an Id of 2 exists in this case.
As requested the $metadata. This is for the second example. Hope this helps!
<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>
答案1
得分: 2
默认情况下,Olingo 2.0 JPA处理器的导航属性命名格式为<导航属性名称>Details
,因此例如,如果您有一个名为Location
的连接,则导航属性将被命名为LocationDetails
。
要执行DeepInsert,您可以使用以下有效负载:
{
"version": "1",
"contactNumber": "",
"LocationDetails": {
"id": "",
"version": "",
"CityMunicipalityDetails": {
// 城市类的所有属性
},
"ProvinceDetails": {
// 省份类的所有属性
}
}
}
或者您还可以查看OData 2中$batch
的实现,因为Deep Insert是OData实现的较新版本的一部分,但是Olingo 2.0 JPA处理器也在2.0版本中实现了。
附注:请检查导航属性名称的$metadata
,如果可能的话,请发布$metadata
内容,如果此解决方案无法解决问题。
英文:
The Default Implementaion of Olingo 2.0 JPA processor names the Navigation properties in the format <Navigation Property Name>Details
, so for example if you have a join named Location
the navigation property will be named as LocationDetails
To perform the DeepInset you can use the following payload
{
"version": "1",
"contactNumber" : "",
"LocationDetails": {
"id":"",
"version":"",
"CityMunicipalityDetails":{
// all attributes of city class
},
"ProvinceDetails":{
// all attributes of province class
}
}
}
Alternatively you can also look at the implementation of $batch
in OData 2 as Deep inserts were part of Later Version of OData Implementations but Olingo 2.0 JPA Processor implemented in version 2.0 also.
PS: Please check the $metadata
for the navigation property names and if possible post the $metadata
content if this solution doesn't solves the issue.
答案2
得分: 0
你已经指定了两个不起作用的POST请求。对于第一个请求,下面的请求体应该可以工作。
{
"version": "1",
"companyProfile": {
"id": 1,
"version": "1"
}
}
对于第二个请求,
{
"version": "1",
"contactNumber": "",
"address": {
"id": "",
"version": "",
"city": {
// 城市类的所有属性
},
"province": {
// 省份类的所有属性
}
}
}
英文:
You have specified two post request which is not working. For the first one, below request body should work.
{
"version": "1",
"companyProfile": {
"id" : 1,
"version" : "1"
}
}
For the second one,
{
"version": "1",
"contactNumber" : "",
"address": {
"id":"",
"version":"",
"city":{
// all attributes of city class
},
"province":{
// all attributes of province class
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论