Objectify: Filter by Ref (or Key) of a Certain Kind

北城余情 提交于 2019-12-23 20:24:06

问题


Say I have these classes:

@Entity class MyEntity {
    @Id String id;
    @Index Ref<?> ref;
}

@Entity class Kind2 {
    ...
}

Can I query for all MyEntitiy objects where ref refers to any instance of Kind2? If so, how?


回答1:


Moshe's answer is really the right one. However, you can technically hack something that works by performing inequality queries on the key. Ie, >= KEY('Kind2', 0) and <= KEY('Kind2', MAX_LONG). This gets significantly more complicated if your entities have parents.

I wouldn't recommend doing this unless you really know what you are doing.




回答2:


Not possible

I think something with the structure of your data maybe flawed. even when you forget the datastore, and just use instance of in plain java, a lot ot times it's a sign that the structure is not right.

But in any case, remember that when working with the datastore, you need to index the things you query. so if you want to query for a ref kind, figure out a way to index it. probably another property on the MyEntity is the way to go.




回答3:


I achieve it this way:

ofy().load().type(MyEntity.class).filter("ref =",Ref.create(new Kind2(kind2Id))).list();

Adding @Index to the ref property as you did and that's it.

It will retrieve it filtered.



来源:https://stackoverflow.com/questions/22391109/objectify-filter-by-ref-or-key-of-a-certain-kind

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