【问题标题】:Objectify - The list from query result contains nullObjectify - 查询结果中的列表包含 null
【发布时间】:2014-01-10 05:41:08
【问题描述】:

当我尝试下面的代码时,“结果”包含 null。

    Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list();

result.size() 不为零。

“结果”中包含 null 的原因是什么?

【问题讨论】:

  • 请在下面查看我的答案。您在 ofy() 和 type() 之间缺少一个加载方法。
  • @AnkurJain 这是我的错字。对不起。我更新了我的问题。
  • 设置 .cache(false) 后工作正常。我不知道为什么它是解决方案。我需要进一步研究。
  • 这看起来更像是一个理解 java 集合如何工作的问题。 result.size() 是否与列表中非空元素的数量相同?如果列表的容量大于其大小,则列表可以包含空元素。但这不应该是一个问题......使用迭代器迭代结果,您无需担心它: List all = ofy().load().type(Data.class).list() ;迭代器 it = all.iterator(); while(it.hasNext()){数据数据 = it.next();}
  • 是的。那是我的好奇。太荒谬了!我不知道 Collection 可以包含 null!

标签: java google-app-engine objectify


【解决方案1】:

请查看以下代码,描​​述了 Objectify 中的所有基本操作。

获取Objectify中所有对象的列表

List<T> list = ofy().load().type(clazz).list();

您还可以在下面的代码中使用 GenericDAO 及其实现在 Objectify 中找到一些额外的查询。

通用DAO接口

package com.myweb.webservices.common.dao;

import java.util.List;

import com.myweb.webservices.common.exception.DatabaseException;

public interface GenericDao {
public <T> void create(T t);
public <T> String createWithKey(T t);
public <T> Long createWithID(T t);
public <T> void update(Class<T> clazz, Long id, T t) throws DatabaseException;
public <T> void update(Class<T> clazz, String key, T t) throws DatabaseException;
public <T> T getById(Class<T> clazz, Long id) throws DatabaseException;
public <T> T getByKey(Class<T> clazz, String key) throws DatabaseException;
public <T> List<T> list(Class<T> clazz);
public <T> void delete(Class<T> clazz, Long id) throws DatabaseException;
public <T> void deleteByKey(Class<T> clazz, String key) throws DatabaseException;   
}

GenericDAO 实施

package com.myweb.webservices.common.dao;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.myweb.webservices.common.exception.DatabaseException;
import com.myweb.webservices.model.User;

public abstract class AbstractGenericDaoImpl implements GenericDao{

static {
    ObjectifyService.register(User.class);
}

@Override
public <T> void create(T t) {
    ofy().save().entity(t).now();
}

@Override
public <T> String createWithKey(T t) {
    Key<T> key =  ofy().save().entity(t).now();
    return key.getString();
}

@Override
public <T> Long createWithID(T t) {
    Key<T> key =  ofy().save().entity(t).now();
    return key.getId();
}

@Override
public <T> void update(Class<T> clazz, Long id, T t) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T tnew = ofy().load().type(clazz).id(id).get();
    ofy().save().entity(tnew).now();
}

@Override
public <T> void update(Class<T> clazz, String key, T t) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T tnew = ofy().load().type(clazz).id(key).get();
    ofy().save().entity(tnew).now();

}

@Override
public <T> T getById(Class<T> clazz, Long id) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    return ofy().load().type(clazz).id(id).get();
}

@Override
public <T> T getByKey(Class<T> clazz, String key) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    return ofy().load().type(clazz).id(key).get();
}

@Override
public <T> List<T> list(Class<T> clazz) {
    List<T> list = ofy().load().type(clazz).list();
    return list;
}

@Override
public <T> void delete(Class<T> clazz, Long id) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T t = ofy().load().type(clazz).id(id).get();
    if(t != null){
        ofy().delete().entity(t).now();
    }
}

@Override
public <T> void deleteByKey(Class<T> clazz, String key) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T t = ofy().load().type(clazz).id(key).get();
    if(t != null){
        ofy().delete().entity(t).now();
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2020-11-14
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多