user411103
user411103

Reputation:

Queries with Objectify: UmbrellaException

I am using Objectify to manage GAE Datastore for my GWT app. The problem is that I am not using queries properly and I get UmbrellaExceptions as per below:

Caused by: java.lang.RuntimeException: Server Error: java.lang.String cannot be cast to java.lang.Number
    at com.google.web.bindery.requestfactory.shared.Receiver.onFailure(Receiver.java:44)

Say that I have a class Box with a unique field String id. I want to get the Box object whose id == "cHVQP6zZiUjM"

This is how I do it now:

public Box getBox(String boxId)
{
    Objectify ofy = ObjectifyService.begin();
    Query<Box> q=ofy.query(Box.class).filter("id",boxId);
    Box targetBox = q.get();

    return targetBox;
}


@Entity
public class Box extends DatastoreObject{
    private String id;
    private String title;
}

I tried doing this with ofy.load() but that method is not defined in my class Objectify (I don't know why).

Upvotes: 0

Views: 182

Answers (2)

stickfigure
stickfigure

Reputation: 13556

The short answer: You are missing the @Id annotation in your entity.

The long answer: Id fields are special in the datastore. The id is not a real property, but rather a part of the Key that identifies the entity. You can't really filter on id fields, but you can filter on a special field called __key__. Objectify is somewhat clever about letting you filter by the id field and converting this to a __key__ filter under the covers, but it can't do it if you don't annotate the entity properly!

Actually I'm a little confused because Objectify shouldn't let you register the entity without an @Id field.

By the way, there are two sections of the documentation: Objectify4 (release coming soon) and Objectify3. Since you're using Ofy3, there is no load() method.

Another thing: Get-by-key operations are strongly preferred to queries when the operations are equivalent (as they are in your example).

Upvotes: 0

Gilberto Torrezan
Gilberto Torrezan

Reputation: 5267

Your key is encoded. Try using:

 Box targetBox = ofy.get(Box.class, KeyFactory.stringToKey(boxId));

To decode your key.

Upvotes: 2

Related Questions