【问题标题】:How to retrieve related Odata entities Odata4j如何检索相关的Odata实体Odata4j
【发布时间】:2013-10-07 11:32:23
【问题描述】:

我在使用 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();

【问题讨论】:

    标签: java odata relationship odata4j


    【解决方案1】:

    您是否尝试过getRelatedEntities() 而不是getRelatedEntity()。根据您的解释,我了解到 A 有 B 的集合,所以请尝试

    List&lt;OEntity&gt; bsOEntities = a.getLink("bs", OLink.class).getRelatedEntities();

    它对我有用。

    【讨论】:

    • 嗨,对不起,我现在才看到这个。是的,我们也试过了,但没有奏效。据我记得,这个问题最终出现在我们的注释中。我们也试图以错误的方式看待这些关系。不过感谢您的回答:)
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多