3
votes

I am trying to retrieve a sorted list of the players scores from a datastore Entity of type"User" After executing:

 List<User> entities = ofy().load().type(User.class).order("-score").list();

Knowing that I indexed the "score" attribute. Here is the "User.class"

@Id String userName;
     String password;
    @Index int score;

The entities seem to be null and i can't get the entities details.What am I missing???

I am still working on that I managed to modify the method to:

 @Override
  public User display(){
     List<User> list = ofy().load().type(User.class).order("-score").limit(5).list();
     User u= list.get(list.size()-1);
      if(!u.getUserName().equals(null))  
        return  u;
      return null;
  }

I managed to get rid of the "null" value by checking the username field but the return value was always the first user in the entity no matter how I changed list.get(index).I tried different values for index but always received the first user and can't retrieve the others. Hoping to get an answer that would solve the problem.

The entities are stored in the database as follows:

public User registerAccount(String username, String password,String confirm) {
        User user = ofy().load().type(User.class).id(username).get();



        if(user == null){
            if(password.contains(" "))          
                return null;            
            if(password.compareTo(confirm)!=0)
                return null;

            else{
            user = new User(username,password,0);
            ofy().save().entity(user).now();
            return user;
            }
         }

        else
        {
            return null;
        }
  } 

So is there any problem in using ofy().save()... rather than datastore.put()... I am wondering if that would affect that as the score is not found in the datastore indexes.

1
Is this local dev server or on a running app? Did you verify in datastore viewer that the entities you're looking for exist? - Isaac
Yes sure the entities exist.I checked the datastore viewer and they exist.But I used ofy().save().entity(user).now(); to save an entity of type User rather than using datastore.put().So do you have an idea if this might affect the query? - user3008437
What do you mean "the entities seem to be null"? The list is empty? You're getting a list of nulls back from the query? The entities have null fields? - stickfigure
@stickfigure Please check my edit.Hope u have an idea of the strange results coming up. - user3008437
There still isn't enough information here to understand what is going on. Try to construct a complete test case, including how you save the original data. - stickfigure

1 Answers

1
votes

One possible reason is that @Index annotation on "score" property was not there from the beginning. Users that were added before update, can't be fetched by .order("-score") command.

Another possible thing is trivial but common! It is your class naming ("User"). Do you have correct import? GAE also has quite common class com.google.appengine.api.users.User that is used for authentication.