简体   繁体   English

OData V4 中的双向导航

[英]Bi-directional navigation in OData V4

* I will accept the answer with bounty if it just solves my problem (if you don't have a detailed canonical solution) * *如果它只是解决了我的问题,我将接受赏金的答案(如果您没有详细的规范解决方案)*

When i try to access the metadata from browser ( http://......Service.svc/ $metadata), I see below error.当我尝试从浏览器访问元数据时( http://......Service.svc/ $metadata),我看到以下错误。

500 Cannot find navigation property with name: projectConfigs at type ProjectConfig 500找不到名称为:ProjectConfig 类型的 ProjectConfigs 的导航属性

I am trying to design a bi-directional association in OData v4 (I am using Partner, any other way available?.).我正在尝试在 OData v4 中设计一个双向关联(我正在使用合作伙伴,还有其他可用的方式吗?)。 I am not sure what is the mistake I am doing while implementing it.我不确定我在实施它时犯了什么错误。

I have two classes namely “Project” and “ProjectConfig”.我有两个类,即“Project”和“ProjectConfig”。 I need navigation from Project to ProjectConfig and vice-versa.我需要从 Project 导航到 ProjectConfig,反之亦然。 The idea is that for a defined Project, I should be able to see the ProjectConfig(urations) and from there I want navigation back to the Project it belongs to.这个想法是,对于已定义的项目,我应该能够看到 ProjectConfig(urations) 并从那里导航回它所属的项目。

I am using Olingo framework for writing application code.我正在使用 Olingo 框架来编写应用程序代码。 This is the example i followed. 这是我遵循的示例。

The challenge i see in this example is that navigation name "Products" and EntitySet name "Products" are same.我在此示例中看到的挑战是导航名称“Products”和 EntitySet 名称“Products”相同。

To my understanding when we define partner on a navigation, we should be able to find a property with the same name in the "Nav Type".据我了解,当我们在导航上定义合作伙伴时,我们应该能够在“导航类型”中找到同名的属性。 This will ideally setup the navigation back to the entitytype.理想情况下,这将设置导航回实体类型。

I have pasted the metadata and java application code that is of interest.我已经粘贴了感兴趣的元数据和 java 应用程序代码。

metadata.xml元数据.xml

<EntityType Name="Project">
    <Key>
        <PropertyRef Name="id"/>
    </Key>
    <Property Name="id" Type="Edm.Int32"/>
    <Property Name="name_artifact_id" Type="Edm.String"/>
    <Property Name="groupid" Type="Edm.String"/>
    <Property Name="project_display_name" Type="Edm.String"/>

    <NavigationProperty Name="projectConfigs"
                        Type="Collection(devplatform.config.ProjectConfig)"
                        Partner="project"/>
</EntityType>

<EntityType Name="ProjectConfig">
    <Key>
        <PropertyRef Name="id"/>
    </Key>
    <Property Name="id" Type="Edm.Int32"/>

    <NavigationProperty Name="project"
                        Type="devplatform.config.Project"
                        Partner="projectConfigs"/>
</EntityType>

<EntitySet Name="Projects" EntityType="devplatform.config.Project">
    <NavigationPropertyBinding Path="ProjectConfigs" Target="ProjectConfigs"/>
</EntitySet>

<EntitySet Name="ProjectConfigs" EntityType="devplatform.config.ProjectConfig">
    <NavigationPropertyBinding Path="Projects" Target="Projects"/>
</EntitySet>

DemoEdmProvider.java DemoEdmProvider.java

public static void main(String[] args) {
public static final String ET_PROJECT_NAME = "Project";
public static final FullQualifiedName ET_PROJECT_FQN = 
        new FullQualifiedName(NAMESPACE, ET_PROJECT_NAME);

public static final String ET_PROJECTCONFIG_NAME = "ProjectConfig";
public static final FullQualifiedName ET_PROJECTCONFIG_FQN = 
        new FullQualifiedName(NAMESPACE, ET_PROJECTCONFIG_NAME);

public static final String ES_PROJECTS_NAME = "Projects";
public static final String ES_PROJECTCONFIGS_NAME = "ProjectConfigs";

public static final String NAV_TO_PROJECT = "Project";
public static final String NAV_TO_PROJECTCONFIG = "ProjectConfig";


if (entityTypeName.equals(ET_PROJECT_FQN)) {
    List<CsdlProperty> propertyList = new ArrayList<CsdlProperty>();
    // create EntityType properties
    CsdlProperty id = 
            new CsdlProperty().setName("id")
                              .setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName());
    propertyList.add(id);
    CsdlProperty name_artifact_id = 
            new CsdlProperty().setName("name_artifact_id")
                              .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
    propertyList.add(name_artifact_id);
    CsdlProperty groupid = 
            new CsdlProperty().setName("groupid")
                              .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
    propertyList.add(groupid);
    CsdlProperty project_display_name = 
            new CsdlProperty().setName("project_display_name")
                              .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
    propertyList.add(project_display_name);


    // create PropertyRef for Key element
    CsdlPropertyRef propertyRef = new CsdlPropertyRef();
    propertyRef.setName("id");

    // navigation property: many-to-one, null not allowed (product must have a category)
    List<CsdlNavigationProperty> navPropList = new ArrayList<CsdlNavigationProperty>();

    CsdlNavigationProperty projectconfigs = 
            new CsdlNavigationProperty().setName(NAV_TO_PROJECTCONFIGS)
                                        .setType(ET_PROJECTCONFIG_FQN)
                                        .setCollection(true)
                                        .setPartner("projectConfigs");
    navPropList.add(projectconfigs);


    // configure EntityType
    entityType = new CsdlEntityType();
    entityType.setName(ET_PROJECT_NAME);
    entityType.setProperties(propertyList);
    entityType.setKey(Arrays.asList(propertyRef));
    entityType.setNavigationProperties(navPropList);
}

if (entityTypeName.equals(ET_PROJECTCONFIG_FQN)) {
    List<CsdlProperty> propertyList = new ArrayList<CsdlProperty>();
    // create EntityType properties
    CsdlProperty id = 
            new CsdlProperty().setName("id")
                              .setType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName());
    propertyList.add(id);

    // create PropertyRef for Key element
    CsdlPropertyRef propertyRef = new CsdlPropertyRef();
    propertyRef.setName("id");

    // navigation property: many-to-one, null not allowed (product must have a category)
    List<CsdlNavigationProperty> navPropList = new ArrayList<CsdlNavigationProperty>();

    // ERROR CAUSING LINE
    CsdlNavigationProperty project = 
            new CsdlNavigationProperty().setName(NAV_TO_PROJECT)
                                        .setType(ET_PROJECT_FQN)
                                        .setNullable(true)
                                        .setPartner("project");
    navPropList.add(project);

    // configure EntityType
    entityType = new CsdlEntityType();
    entityType.setName(ET_PROJECTCONFIG_NAME);
    entityType.setProperties(propertyList);
    entityType.setKey(Arrays.asList(propertyRef));
    entityType.setNavigationProperties(navPropList);
}

I can provide any missing details if needed: ,) Could not get responses!如果需要,我可以提供任何缺失的详细信息:,) 无法得到回复! Not sure, if my question is relevant to a small community!!不确定,如果我的问题与小社区有关!!

I guess the root of the issue is in the different case of characters used for NavigationProperty names and NavigationPropertyBinding paths.我猜问题的根源在于用于 NavigationProperty 名称和 NavigationPropertyBinding 路径的字符的不同情况。 Consider using upper camel case for all property names.考虑对所有属性名称使用大写驼峰式大小写。

There is also another problem in using path Projects (in plural) for navigation property binding of the entity set ProjectConfigs while naming the navigation property of the ProjectConfig as project (in singular).在将ProjectConfig的导航属性命名为project (单数)时,使用路径Projects (复数)来绑定实体集ProjectConfigs的导航属性还有另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM