繁体   English   中英

如何检索相关的Odata实体Odata4j

[英]How to retrieve related Odata entities Odata4j

我在使用odata4j库检索相关实体时遇到麻烦。 我的问题如下:

A与B具有一对多的关系。A具有类型B的项目的列表“ bs”。我创建了实体A,并使用

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();

然后,我使用以下方法检索实体B,其中convert将检索到的对象的属性分配给类型A的对象:

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

在转换中,我尝试使用以下方法获取相关实体:

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

上面的结果导致检索到链接,但是“ getRelatedEntity”返回null

我使用链接和相关实体是否错误? 如果是这样,我将如何在Odata4j中检索相关实体? 在线上没有很多示例。

您的帮助将不胜感激。

谢谢

编辑:我也尝试使用以下方法检索相关实体:

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

而且我尝试使用创建链接,该方法似乎以相同的方式工作,但需要额外调用以获取实体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();

您是否尝试过使用getRelatedEntities()而不是getRelatedEntity() 根据您的解释,我知道A具有B的集合,因此请尝试

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

这个对我有用。

暂无
暂无

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

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