问题
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