数据存储查询返回null [英] Datastore Query returns null

查看:129
本文介绍了数据存储查询返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据存储中检索玩家得分的排序列表,类型为User的实体
执行完毕后:

 列表<用户>实体= ofy().load().type(User.class).order( -  score).list(); 

知道我已将分数属性编入索引。
这里是User.class

  @Id String userName; 
字符串密码;
@Index int分数;

实体似乎为空,我无法获取实体细节。我错过了什么?



我仍在努力,我设法修改方法为:

  @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;
返回null;
}

通过检查用户名字段,我设法摆脱了null但返回值始终是实体中的第一个用户,不管我如何更改list.get(index)。我尝试了不同的索引值,但始终接收第一个用户,并且无法检索其他用户。
希望得到一个可以解决问题的答案。



实体存储在数据库中,如下所示:

  public User registerAccount(String username,String password,String confirm){
User user = ofy()。load()。type(User.class)。 ID(用户名)获得();


$ b 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();
返回用户;
}
}

else
{
return null;






$ b

所以使用ofy()会有什么问题。 save()...而不是datastore.put()...
我想知道这是否会影响到,因为在数据存储索引中找不到分数。

解决方案

一个可能的原因是score属性上的@Index注释不是从一开始就存在的。更新之前添加的用户无法通过.order( - score)命令获取。

另一个可能的事情是微不足道的,但是很常见!这是你的班级命名(用户)。你有正确的进口? GAE也有相当常见的com.google.appengine.api.users.User类用于认证。


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.

这篇关于数据存储查询返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆