`

OdataDeepInsert

 
阅读更多
Deep Inserts

OData already supports 'deep inserts'. A 'deep insert' creates an entity and builds a link to an existing entity. So posting here:

~/People(6)/Friends

Inserts a new person and creates a 'friends' link with Person 6.

The same type specification rules apply for deep inserts too, so posting here:

~/People(6)/Friends/HR.Employee

Will succeed only if the type is unambiguous and allowed (i.e. it is an Employee or derived from Employee).

如果你想寻找灵感,可以参考以下代码片段

public void simpleDeepInsert() throws Exception {
    final ODataClient client = getEdmEnabledClient();
    final URI createURI = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).build();
    final ClientObjectFactory factory = getFactory();
    final ClientEntity entity = factory.newEntity(ET_KEY_NAV);

    // Prepare entity(EntitySet: ESKeyNav, Type: ETKeyNav)
    entity.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_INT16, factory.newPrimitiveValueBuilder().buildInt16((short) 42)));
    entity.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("42")));
    entity.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_NAV,
            factory.newComplexValue(CT_NAV_FIVE_PROP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 42)))));
    entity.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_ALL_PRIM,
            factory.newComplexValue(CT_ALL_PRIM)
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("42")))));
    entity.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_TWO_PRIM,
            factory.newComplexValue(CT_TWO_PRIM)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 42)))
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("42")))));
    entity.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_COMP_NAV,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("42")))
                .add(factory.newComplexProperty(PROPERTY_COMP_NAV,
                    factory.newComplexValue(CT_NAV_FIVE_PROP)
                        .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                            factory.newPrimitiveValueBuilder().buildInt16((short) 42)))))));

    // Non collection navigation property
    // Create related entity(EntitySet: ESTwoKeyNav, Type: ETTwoKeyNav, Nav. Property: NavPropertyETTwoKeyNavOne)
    final ClientEntity inlineEntitySingle = factory.newEntity(ET_TWO_KEY_NAV);
    inlineEntitySingle.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_INT16, factory.newPrimitiveValueBuilder().buildInt16((short) 43)));
    inlineEntitySingle.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("43")));
    inlineEntitySingle.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 430)))));
    inlineEntitySingle.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_NAV,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 431)))));
    inlineEntitySingle.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_TWO_PRIM,
            factory.newComplexValue(CT_TWO_PRIM)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 432)))
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("432")))));

    // Collection navigation property
    // The navigation property has a partner navigation property named "NavPropertyETKeyNavOne"
    // Create related entity(EntitySet: ESTwoKeyNav, Type: NavPropertyETTwoKeyNavMany
    final ClientEntity inlineEntityCol1 = factory.newEntity(ET_TWO_KEY_NAV);
    inlineEntityCol1.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_INT16, factory.newPrimitiveValueBuilder().buildInt16((short) 44)));
    inlineEntityCol1.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("44")));
    inlineEntityCol1.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_NAV,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 441)))));
    inlineEntityCol1.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 440)))));
    inlineEntityCol1.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_TWO_PRIM,
            factory.newComplexValue(CT_TWO_PRIM)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 442)))
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("442")))));

    final ClientEntity inlineEntityCol2 = factory.newEntity(ET_TWO_KEY_NAV);
    inlineEntityCol2.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_INT16, factory.newPrimitiveValueBuilder().buildInt16((short) 45)));
    inlineEntityCol2.getProperties()
        .add(factory.newPrimitiveProperty(PROPERTY_STRING, factory.newPrimitiveValueBuilder().buildString("45")));
    inlineEntityCol2.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_NAV,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 451)))));
    inlineEntityCol2.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP,
            factory.newComplexValue(CT_PRIM_COMP)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 450)))));
    inlineEntityCol2.getProperties()
        .add(factory.newComplexProperty(PROPERTY_COMP_TWO_PRIM,
            factory.newComplexValue(CT_TWO_PRIM)
                .add(factory.newPrimitiveProperty(PROPERTY_INT16,
                    factory.newPrimitiveValueBuilder().buildInt16((short) 452)))
                .add(factory.newPrimitiveProperty(PROPERTY_STRING,
                    factory.newPrimitiveValueBuilder().buildString("452")))));

    final ClientInlineEntity newDeepInsertEntityLink =
        factory.newDeepInsertEntity(NAV_PROPERTY_ET_TWO_KEY_NAV_ONE, inlineEntitySingle);
    final ClientEntitySet newDeepInsertEntitySet = factory.newEntitySet();
    newDeepInsertEntitySet.getEntities().add(inlineEntityCol1);
    newDeepInsertEntitySet.getEntities().add(inlineEntityCol2);
    final ClientInlineEntitySet newDeepInsertEntitySetLink =
        factory.newDeepInsertEntitySet(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY, newDeepInsertEntitySet);

    entity.addLink(newDeepInsertEntityLink);
    entity.addLink(newDeepInsertEntitySetLink);

    // Perform create request
    final ODataEntityCreateResponse<ClientEntity> responseCreate = client.getCUDRequestFactory()
        .getEntityCreateRequest(createURI, entity)
        .execute();
    assertEquals(HttpStatusCode.CREATED.getStatusCode(), responseCreate.getStatusCode());

    final String cookie = responseCreate.getHeader(HttpHeader.SET_COOKIE).toString();

    // Fetch ESKeyNav entity with expand of NavPropertyETTwoKeyNavOne nav. property
    ClientProperty propertyInt16 = responseCreate.getBody().getProperty(PROPERTY_INT16);
    final URI esKeyNavURI =
        client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).appendKeySegment(
            propertyInt16.getPrimitiveValue().toValue()).expand(NAV_PROPERTY_ET_TWO_KEY_NAV_ONE,
                NAV_PROPERTY_ET_TWO_KEY_NAV_MANY).build();

    final ODataEntityRequest<ClientEntity> esKeyNavRequest = client.getRetrieveRequestFactory()
        .getEntityRequest(esKeyNavURI);
    esKeyNavRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> esKeyNavResponse = esKeyNavRequest.execute();

    final ClientEntity clientEntity = esKeyNavResponse.getBody();
    // Check nav. property NavPropertyETTwoKeyNavOne
    assertNotNull(clientEntity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_ONE));
    ClientInlineEntity navOne = ((ClientInlineEntity) clientEntity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_ONE));
    assertShortOrInt(431, navOne.getEntity().getProperty(PROPERTY_COMP_NAV).getComplexValue()
        .get(PROPERTY_INT16).getPrimitiveValue().toValue());

    // Check nav. property NavPropertyETTwoKeyNavMany
    assertNotNull(clientEntity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY));
    ClientInlineEntitySet navMany = (ClientInlineEntitySet)
        clientEntity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY);
    assertEquals(2, navMany.getEntitySet().getEntities().size());
    
    assertShortOrInt(441, navMany.getEntitySet().getEntities().get(0).getProperty(PROPERTY_COMP_NAV)
        .getValue().asComplex().get(PROPERTY_INT16).getPrimitiveValue().toValue());
    assertShortOrInt(451, navMany.getEntitySet().getEntities().get(1).getProperty(PROPERTY_COMP_NAV)
        .getValue().asComplex().get(PROPERTY_INT16).getPrimitiveValue().toValue());

    // Fetch ESTwoKeyNav entities and check if available and the partner relation have been set up
    // Check ESTwoKeyNav(Created via NavPropertyETTwoKeyNavOne)
    Map<String, Object> composedKey = new HashMap<String, Object>();
    composedKey.put(PROPERTY_INT16, navOne.getEntity().getProperty(PROPERTY_INT16).getPrimitiveValue().toValue());
    composedKey.put(PROPERTY_STRING, navOne.getEntity().getProperty(PROPERTY_STRING).getPrimitiveValue().toValue());

    final URI esTwoKeyNavEntitySingleURI = client.newURIBuilder(SERVICE_URI)
        .appendEntitySetSegment(ES_TWO_KEY_NAV)
        .appendKeySegment(composedKey)
        .build();
    final ODataEntityRequest<ClientEntity> esTwoKeyNavSingleRequest = client.getRetrieveRequestFactory()
        .getEntityRequest(esTwoKeyNavEntitySingleURI);
    esTwoKeyNavSingleRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> esTwoKeyNavSingleResponse = esTwoKeyNavSingleRequest.execute();
    assertShortOrInt(431, esTwoKeyNavSingleResponse.getBody().getProperty(PROPERTY_COMP_NAV).getComplexValue()
        .get(PROPERTY_INT16).getPrimitiveValue().toValue());

    // Check ESTwoKeyNav(Created via NavPropertyETTwoKeyNavMany(0))
    composedKey.clear();
    composedKey.put(PROPERTY_INT16, navMany.getEntitySet().getEntities().get(0).getProperty(PROPERTY_INT16)
        .getPrimitiveValue().toValue());
    composedKey.put(PROPERTY_STRING, navMany.getEntitySet().getEntities().get(0).getProperty(PROPERTY_STRING)
        .getPrimitiveValue().toValue());

    URI esTwoKeyNavEntityManyOneURI =
        client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).appendKeySegment(composedKey)
        .expand(NAV_PROPERTY_ET_KEY_NAV_ONE).build();

    final ODataEntityRequest<ClientEntity> esTwoKeyNavManyOneRequest =
        client.getRetrieveRequestFactory().getEntityRequest(esTwoKeyNavEntityManyOneURI);
    esTwoKeyNavManyOneRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> esTwoKeyNavManyOneResponse = esTwoKeyNavManyOneRequest.execute();

    assertShortOrInt(441, esTwoKeyNavManyOneResponse.getBody().getProperty(PROPERTY_COMP_NAV)
        .getComplexValue().get(PROPERTY_INT16).getPrimitiveValue().toValue());
    
    assertNotNull(esTwoKeyNavManyOneResponse.getBody().getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE));
    ClientInlineEntity nvLink = (ClientInlineEntity)esTwoKeyNavManyOneResponse.getBody()
        .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE);
    assertEquals(propertyInt16.getPrimitiveValue().toValue(), nvLink.getEntity().getProperty(PROPERTY_INT16)
        .getPrimitiveValue().toValue());

    // Check ESTwoKeyNav(Created via NavPropertyETTwoKeyNavMany(1))
    composedKey.clear();
    composedKey.put(PROPERTY_INT16, navMany.getEntitySet().getEntities().get(1).getProperty(PROPERTY_INT16)
        .getPrimitiveValue().toValue());
    composedKey.put(PROPERTY_STRING, navMany.getEntitySet().getEntities().get(1).getProperty(PROPERTY_STRING)
        .getPrimitiveValue().toValue());

    URI esTwoKeyNavEntityManyTwoURI =
        client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).appendKeySegment(composedKey)
        .expand(NAV_PROPERTY_ET_KEY_NAV_ONE).build();

    final ODataEntityRequest<ClientEntity> esTwoKeyNavManyTwoRequest =
        client.getRetrieveRequestFactory().getEntityRequest(esTwoKeyNavEntityManyTwoURI);
    esTwoKeyNavManyTwoRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> esTwoKeyNavManyTwoResponse = esTwoKeyNavManyTwoRequest.execute();

    assertShortOrInt(451, esTwoKeyNavManyTwoResponse.getBody().getProperty(PROPERTY_COMP_NAV)
        .getComplexValue().get(PROPERTY_INT16).getPrimitiveValue().toValue());
    assertNotNull(esTwoKeyNavManyTwoResponse.getBody().getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE));
    
    nvLink = (ClientInlineEntity)esTwoKeyNavManyTwoResponse.getBody()
        .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE);
    assertEquals(propertyInt16.getPrimitiveValue().toValue(), nvLink.getEntity().getProperty(PROPERTY_INT16)
        .getPrimitiveValue().toValue());
  }
分享到:
评论

相关推荐

    白色大气风格的旅游酒店企业网站模板.zip

    白色大气风格的旅游酒店企业网站模板.zip

    python实现用户注册

    python实现用户注册

    【图像压缩】基于matlab GUI Haar小波变换图像压缩(含PSNR)【含Matlab源码 9979期】.zip

    Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作

    (177354822)java小鸟游戏.zip

    内容来源于网络分享,如有侵权请联系我删除。另外如果没有积分的同学需要下载,请私信我。

    VB+access学生管理系统(论文+系统)(2024am).7z

    1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于计算机科学与技术等相关专业,更为适合;

    数学计算中的平方表与圆周率π的应用

    内容概要:文档名为《平方表,派表集合.docx》,主要内容是1至1000的平方值以及1至1000与π的乘积结果。每个数字从1开始,逐步增加至1000,对应地计算了平方值和乘以π后的值。所有计算均通过Python脚本完成,并在文档中列出了详细的计算结果。 适合人群:需要进行数学计算或程序验证的学生、教师和研究人员。 使用场景及目标:用于快速查找特定数字的平方值或其与π的乘积,适用于教学、科研及程序测试等场景。 阅读建议:可以直接查阅所需的具体数值,无需从头到尾逐行阅读。建议在使用时配合相应的计算工具,以验证和拓展数据的应用范围。

    VB+SQL光盘信息管理系统(源代码+系统+答辩PPT)(20244m).7z

    1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于计算机科学与技术等相关专业,更为适合;

    白色大气风格的健身私人教练模板下载.zip

    白色大气风格的健身私人教练模板下载.zip

    白色简洁风的商务网站模板下载.zip

    白色简洁风的商务网站模板下载.zip

    白色大气风格的前端设计案例展示模板.zip

    白色大气风格的前端设计案例展示模板.zip

    圣诞树项目中的硬件和MATLAB实现指南

    内容概要:本文介绍了两个有趣的圣诞树项目方向:一是使用Arduino或Raspberry Pi开发可编程的圣诞树灯光控制系统;二是基于MATLAB开发一个圣诞树模拟器。前者通过硬件连接、编写Arduino/Raspberry Pi程序、MATLAB控制程序来实现LED灯带的闪烁;后者则通过创建圣诞树图形、添加动画效果、用户交互功能来实现虚拟的圣诞树效果。 适合人群:具备基本电子工程和编程基础的爱好者和学生。 使用场景及目标:①通过硬件和MATLAB的结合,实现实际的圣诞树灯光控制系统;②通过MATLAB模拟器,实现一个有趣的圣诞树动画展示。 阅读建议:读者可以根据自己的兴趣选择合适的项目方向,并按照步骤进行动手实践,加深对硬件编程和MATLAB编程的理解。

    白色扁平风格的温室大棚公司企业网站源码下载.zip

    白色扁平风格的温室大棚公司企业网站源码下载.zip

    Navicat.zip

    Navicat.zip

    Scikit-learn库中主成分分析(PCA)技术的Python实现教程

    内容概要:本文详细介绍了主成分分析(PCA)技术的原理及其在Scikit-learn库中的Python实现。首先讲解了PCA的基本概念和作用,接着通过具体示例展示了如何使用Scikit-learn进行PCA降维。内容涵盖了数据准备、模型训练、数据降维、逆转换数据等步骤,并通过可视化和实际应用案例展示了PCA的效果。最后讨论了PCA的局限性和参数调整方法。 适合人群:数据科学家、机器学习工程师、数据分析从业者及科研人员。 使用场景及目标:适用于高维数据处理,特别是在需要降维以简化数据结构、提高模型性能的场景中。具体目标包括减少计算复杂度、提高数据可视化效果和改进模型训练速度。 其他说明:本文不仅提供了详细的代码示例,还讨论了PCA在手写数字识别和机器学习模型中的应用。通过比较原始数据和降维后数据的模型性能,读者可以更好地理解PCA的影响。

    (175846434)目标检测-将VOC格式的数据集一键转化为COCO和YOLO格式

    VOC格式的数据集转COCO格式数据集 VOC格式的数据集转YOLO格式数据集。内容来源于网络分享,如有侵权请联系我删除。另外如果没有积分的同学需要下载,请私信我。

    数字信号处理课程设计.doc

    数字信号处理课程设计.doc

    白色扁平化风格的灯饰灯具销售企业网站模板.zip

    白色扁平化风格的灯饰灯具销售企业网站模板.zip

    华豫佰佳组合促销视图.sql

    华豫佰佳组合促销视图.sql

    白色大气风格的商务团队公司模板下载.zip

    白色大气风格的商务团队公司模板下载.zip

    白色大气风格的VPS销售网站模板.zip

    白色大气风格的VPS销售网站模板.zip

Global site tag (gtag.js) - Google Analytics