Objectify sequence query

108 views Asked by At

I try to have such code:

    Query<Card> query = ofy().load().type(Card.class);

    UserData creator = ofy().load().type(UserData.class).id(creatorId).now();
    if (creator != null && UserType.USER.equals(creator.getUserType())) {
        query.filter("creator", creator);
    }
    if (orderColumnName != null) {
        query.order((ascending ? "" : "-") + orderColumnName);
    }
    query.offset(startRow).limit(limit);
    return query.list();

But it doesn't filter. Also this filter:

    UserData creator = ofy().load().type(UserData.class).id(creatorId).now();
    Query<Card> query = ofy().load().type(Card.class).filter("creator", creator);

Any idea why?

EDITED

My class Card.java

@Entity
public class Card implements PersistableObject {

    @Id
    Long id;

    @Index
    Date createDate;

    ...

    @Index
    Ref<UserData> creator;

    ...

    public UserData getCreator() {
        if (creator != null) {
            return creator.get();
        }
        return null;
    }

    public void setCreator(UserData creator) {
        this.creator = Ref.create(creator);
    }
}

My class UserData.java

@Entity
public class UserData implements PersistableObject {

    @Id
    Long id;

    Ref<EaistoAccount> eaistoAccount;

    UserType userType;

    public EaistoAccount getEaistoAccount() {
        if (eaistoAccount == null) {
            return null;
        }
        return eaistoAccount.get();
    }

    public void setEaistoAccount(EaistoAccount aistoAccount) {
        this.eaistoAccount = Ref.create(aistoAccount);
    }
}

It doesn't work means that I expect to get filtered entities to corresponding UserData but it doesn't filter when I split query in a few parts also it filters when I use query in one line.

1

There are 1 answers

0
Alex Po On BEST ANSWER

I have found a solution:

Why aren't my queries working properly? All of Objectify's intermediate command objects are immutable. This will not work:

Query q = ofy().load().type(Foo.class); q.filter("bar", bar); List foos = q.list(); The filter command did nothing because you did not reassign q. You need this:

q = q.filter("bar", bar); Alternatively, chain the whole sequence in a single statement. Read more here.

https://code.google.com/p/objectify-appengine/wiki/FrequentlyAskedQuestions