Search code examples
odata4j

How do I use OData4J with an unknown number of child objects?


I am trying to build a createEntity OEntity for an object that has multiple child collections within it.

I have looked over many of the example projects, but they all seem to assume that you have a known number of child objects in a collection so that you can use .inLine(“ObjectName”, ObjectOEntity1, ObjecteOEntity2…)

I have tried looking at the documentation and have not identified anything that leads me to think I can create a collection of OEntity objects that can then be added to my parent object with the inline.

The closest I found was the example listed on:

http://code.google.com/p/odata4j/source/browse/odata4j-fit/src/test/java/org/odata4j/producer/jpa/northwind/test/CreateTest.java?name=0.6

Has anyone else run into this problem?
If so how did you get around it?


Solution

  • You can pass in an array of OEntity objects. The core4j library that is used by odata4j contains some helper methods that can - for example - be used to get an array from an Iterable:

    OEntity[] entitiesArray = Enumerable.create(entitiesIterable)
        .toArray(OEntity.class);
    

    But as there are also two variants of the properties method...

    OCreateRequest<T> properties(OProperty<?>... props);
    OCreateRequest<T> properties(Iterable<OProperty<?>> props);
    

    ... it might make sense to add an inline method that directly takes an Iterable<OEntity>.