Tomasz Kapłoński
Tomasz Kapłoński

Reputation: 1378

GoogleAppEngine - query with some custom filter

I am quite new with appEnginy and objectify. However I need to fetch a single row from db to get some value from it. I tried to fetch element by ofy().load().type(Branch.class).filter("parent_branch_id", 0).first() but the result is FirstRef(null). However though when I run following loop:

for(Branch b : ofy().load().type(Branch.class).list()) {
    System.out.println(b.id +". "+b.tree_label+" - parent is " +b.parent_branch_id);
};

What do I do wrong?

[edit] Ofcourse Branch is a database entity, if it matters parent_branch_id is of type long.

Upvotes: 0

Views: 120

Answers (3)

Shaun
Shaun

Reputation: 199

Example from the Objectify API reference:

LoadResult<Thing> th = ofy.load().type(Thing.class).filter("foo", foo).first();
Thing th =       ofy.load().type(Thing.class).filter("foo", foo).first().now();

So you need to make sure member "foo" has an @Index and use the now() to fetch the first element. This will return a null if no element is found.

May be "parent_branch_id"in your case is a long, in which case the value must be 0L and not 0.

Upvotes: 0

tx802
tx802

Reputation: 3564

It sounds like you don't have an @Index annotation on your parent_branch_id property. When you do ofy().load().type(Branch.class).list(), Objectify is effectively doing a batch get by kind (like doing Query("Branch") with the low-level API) so it doesn't need the property indexes. As soon as you add a filter(), it uses a query.

Assuming you are using Objectify 4, properties are not indexed by default. You can index all the properties in your entity by adding an @Index annotation to the class. The annotation reference provides useful info.

Upvotes: 0

CMDej
CMDej

Reputation: 294

If you want a Branch as the result of your request, I think you miss a .now():
Branch branch = ofy().load().type(Branch.class).filter("parent_branch_id", 0).first().now();

Upvotes: 1

Related Questions