`

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());
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics