Eric Simonton
Eric Simonton

Reputation: 6029

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

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?

Upvotes: 3

Views: 1428

Answers (3)

Gerardo Mart&#237;nez
Gerardo Mart&#237;nez

Reputation: 863

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.

Upvotes: 2

stickfigure
stickfigure

Reputation: 13556

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.

Upvotes: 2

Moshe Shaham
Moshe Shaham

Reputation: 15984

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.

Upvotes: 2

Related Questions