简体   繁体   中英

Datastore Query returns null

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM