【问题标题】:How to Use ObjectifyServices to get the Model Id instead of Id in java?java中如何使用ObjectifyServices获取模型ID而不是ID?
【发布时间】:2019-06-28 06:43:41
【问题描述】:

我想返回 Entity 的 Id 而不是 EntityMOdel。以下函数将返回MyModel 的列表,但是,我想返回列表,其中该列表将是过滤器MyModel 的ID。

public static List<MyModel> getUpdatedMyModel(Long beforeTime) {
    return ofy().load().type(MyModel.class).filter("updatedAt >", beforeTime).list() 

}

【问题讨论】:

    标签: google-app-engine google-cloud-storage objectify


    【解决方案1】:

    听起来你想要一个只有键的查询:

    final List<Key<MyModel>> keys = ofy().load()
            .type(MyModel.class)
            .filter("updatedAt >", beforeTime)
            .keys()
            .list();
    

    您可以将其转换为带有 Java 流的 id:

    final List<Long> ids = ofy().load()
            .type(MyModel.class)
            .filter("updatedAt >", beforeTime)
            .keys()
            .list()
            .stream()
            .map(Key::getId)
            .collect(Collectors.toList());
    

    但是,在您的应用中传递 Long 值通常是一个坏习惯。 Key&lt;?&gt; 对象是类型安全的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多