Objectify query filter by List of keys that have a parent

∥☆過路亽.° 提交于 2019-12-20 05:48:15

问题


I would like to create an Api method via Google App Engine (Objectify) that returns a CollectionResponse of the posts of the people that I am following, sorted by date descending.

I have an Entity Post and Entity Profile both of which have Long id as their key.

ThePost Entity has the following property specifying it has a Parent:

@Parent
private Key<Profile> profileKey;

The Profile Entity has the following property storing a List of id's of the people the profile is following:

// Ids of the profiles this person follows
private List<Long> following = new ArrayList<>(0);

I was thinking then, I could do something like this:

    List<Long> idsProfile = profile.getFollowing();

    Query<Goal> query = ofy().load().type(Post.class)
                    .order("-createdDate") 
                    .filterKey("in", idsProfile) 
                    .limit(Constants.DEFAULT_LIST_LIMIT);

            if (cursor != null) {
                query = query.startAt(Cursor.fromWebSafeString(cursor));
            }

            QueryResultIterator<Goal> queryIterator = query.iterator();
            List<Post> postList = new ArrayList<Post>(Constants.DEFAULT_LIST_LIMIT);
            while (queryIterator.hasNext()) {
                postList.add(queryIterator.next());
            }

            return CollectionResponse.<Post>builder().setItems(postList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();

What I am doing here is getting a List of all the id's of Profile's that someone is following and trying to query and filter on that List to only return the Post's by those users.

But I am getting this error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "java.lang.IllegalArgumentException: __key__ filter value must be a Key"
   }
  ],
  "code": 400,
  "message": "java.lang.IllegalArgumentException: __key__ filter value must be a Key"
 }
}

I've been trying different things within the .filterKey("in", idsProfile) bit but can't seem to get it to work.

Could someone help me with that part to make this work? Thanks!

来源:https://stackoverflow.com/questions/34747921/objectify-query-filter-by-list-of-keys-that-have-a-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!