2

I am having trouble retrieving related entities using the odata4j library. My problem is as follows:

A has a one to many relationship with B. A has a list, "bs", of items of type B. I created entity A and the link using

Map<String, Object> map = new HashMap<String, Object>();
map.put("id", a.getId());
OEntity entityB = consumer.createEntity("B").properties(OProperties.string("name", "some name")).link("a", OEntityKey.create(map)).execute();

I then retrieved entity B using the following, where convert assigns the properties of the retrieved object to an object of type A:

A b = convert(consumer.getEntity("A", id).expand("bs").execute());

In the conversion I attempted to get the related entities using:

OEntity bsOEntity = a.getLink("bs", OLink.class).getRelatedEntity();

The above resulted in a link being retrieved, but "getRelatedEntity" returns null

Am I using links and related entities wrong? If so, how would I retrieve related entities in Odata4j? There are not many examples online.

Your help would be highly appreciated.

Thank you

Edit: I have also tried retrieving a related entity using:

ORelatedEntitiesLink link = (ORelatedEntitiesLink) a.getLinks().get(0);
OEntity retrievedEntity = consumer.getEntities(link).top(1).execute().first();

And I have tried to create the link using, which seems to work the same way, but with an extra call to get entity B:

OEntity bEntity = consumer.getEntity("A", FOREIGN_KEY_VALUE).execute();
OEntity medEntity = consumer.createEntity("B").properties(OProperties.string("name", "some name")).link("a", bEntity).execute();
Kris
  • 21
  • 3

1 Answers1

0

Have you tried getRelatedEntities() instead of getRelatedEntity(). From your explanation I understand that A has a collection of B, so try

List<OEntity> bsOEntities = a.getLink("bs", OLink.class).getRelatedEntities();

It works for me.

user5252279
  • 53
  • 2
  • 6
  • Hi, sorry I only saw this now. Yes we tried that too and it didn't work. As far as I remember the issue ended up being in our annotations. Also we were trying to look at the relationships the wrong way around. Thanks for the answer though :) – Kris Feb 13 '14 at 07:39