首页 > 解决方案 > 在有关系的实体上发布关于 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> 

标签: javaspring-bootodataolingo

解决方案


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
          }
    
    }
}

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

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


推荐阅读