使用 objectify 在 name/value 个对象的列表 属性 上订购数据存储实体

Order datastore entities on a list property of name/value object with objectify

使用 Google 云数据存储,我想列出我的实体。 我想根据其名称按 属性 的值排序检索它们。

Java class 型号:

public class ContainerEntity {
    @Index
    String id;
    @Index
    private List<NameValueProperty> properties;
}

public class NameValueProperty {
    @Index
    private String name;
    @Index
    private String value;
}

数据集示例:

"items": [
{
   "id": "114191058616700893306-20180416-120212-125-Qotx9AlsSRHg4cJhya",
   "properties": [
    {
     "name": "propertyIWantToOrderBy",
     "value": "BBBB"
    },
    {
     "name": "propertyIDoNotCareTheOrder",
     "value": "1522540800000"
    }
   ]
  },
  {
   "id": "114191058616700893306-20180416-120718-199-Qotx9AlsSRHg4cJhya",
   "properties": [
    {
     "name": "propertyIWantToOrderBy",
     "value": "AAAA"
    },
    {
     "name": "propertyIDoNotCareTheOrder",
     "value": "1522540800000"
    }
   ]
  }
}

我如何使用 objectify 来做到这一点? 我尝试使用这段代码,但它不起作用:

Query<ContainerEntity> query = ObjectifyService.ofy().load().type(ContainerEntity.class);
query = query.filter("properties.name = ", "propertyIWantToOrderBy")
             .order("properties.value");

首先它过滤(我不想要),然后看起来它在名称 "propertyIWantToOrderBy" 上的 属性 上的排序不正确。有没有 solution/workaround 可以做到这一点?想做一些类似 order("properties.value where properties.name='propertyIWantToOrderBy'") 的事情。 这个设计的重点是要有一个动态实体

对象化版本:5.1.22 Jdk: 8

当您尝试过滤和排序列表属性时,它们可能会令人困惑。试试这个方法:

public class ContainerEntity {
    @Id
    String id;
    @Index
    private Map<String, String> properties = new HashMap<>();
}

现在您的查询如下所示:

ofy().load().type(ContainerEntity.class).order("properties.propertyIWantToOrderBy");

顺便说一句,您不需要 @Index ID,这些 ID 不会存储为常规属性。他们需要 @Id 注释。