Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Objectify-查询结果中的列表包含null_Java_Google App Engine_Objectify - Fatal编程技术网

Java Objectify-查询结果中的列表包含null

Java Objectify-查询结果中的列表包含null,java,google-app-engine,objectify,Java,Google App Engine,Objectify,当我尝试下面的代码时,“result”包含null Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list(); Collection result=ObjectifyService.ofy().load().type(Data.class).list(); result.size()不是零 “结果”中包含null的原因是什么?请检查下面的代码描述了Objectify中的所

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

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


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

请检查下面的代码描述了Objectify中的所有基本操作

获取Objectify中所有对象的列表

List<T> list = ofy().load().type(clazz).list();
List List=ofy().load().type(clazz.List();
此外,您还可以在Objectify中使用GenericDAO找到一些其他查询,并在下面的代码中找到其实现

通用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;   
}
包com.myweb.webservices.common.dao;
导入java.util.List;
导入com.myweb.webservices.common.exception.DatabaseException;
公共接口通用DAO{
公共无效创建(T);
公共字符串createWithKey(T);
公共Long-createWithID(T);
公共void更新(Class clazz,Long id,T)抛出数据库异常;
公共void更新(Class clazz,String key,T T)抛出数据库异常;
public T getById(Class clazz,Long id)抛出DatabaseException;
公共T getByKey(类clazz,字符串键)抛出DatabaseException;
公开名单(班级分类);
公共void delete(Class clazz,Long id)抛出DatabaseException;
public void deleteByKey(Class clazz,String key)抛出DatabaseException;
}
通用DAO实现

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();
    }
}
}
包com.myweb.webservices.common.dao;
导入静态com.googlecode.objectify.ObjectifyService.ofy;
导入java.util.List;
导入com.googlecode.objectify.Key;
导入com.googlecode.objectify.ObjectifyService;
导入com.myweb.webservices.common.exception.DatabaseException;
导入com.myweb.webservices.model.User;
公共抽象类AbstractGenericDaoImpl实现GenericDao{
静止的{
ObjectifyService.register(User.class);
}
@凌驾
公共无效创建(T){
of y().save().entity(t).now();
}
@凌驾
公共字符串createWithKey(T){
Key=ofy().save().entity(t).now();
return key.getString();
}
@凌驾
公共长createWithID(T){
Key=ofy().save().entity(t).now();
return key.getId();
}
@凌驾
公共void更新(类clazz,Long id,T)引发DatabaseException{
if(id==null){
抛出新的DatabaseException(“ID不能为null”);
}
T tnew=ofy().load().type(clazz).id(id).get();
ofy().save().entity(tnew.now();
}
@凌驾
公共void更新(类clazz,字符串key,T)引发DatabaseException{
if(key==null){
抛出新的DatabaseException(“ID不能为null”);
}
T tnew=ofy().load().type(clazz).id(key.get();
ofy().save().entity(tnew.now();
}
@凌驾
公共T getById(类clazz,长id)引发DatabaseException{
if(id==null){
抛出新的DatabaseException(“ID不能为null”);
}
返回y().load().type(clazz.id(id.get());
}
@凌驾
public T getByKey(类clazz,字符串键)引发DatabaseException{
if(key==null){
抛出新的DatabaseException(“ID不能为null”);
}
返回y().load().type(clazz.id(key.get());
}
@凌驾
公共列表(课堂){
List List=ofy().load().type(clazz.List();
退货清单;
}
@凌驾
公共void delete(类clazz,长id)引发DatabaseException{
if(id==null){
抛出新的DatabaseException(“ID不能为null”);
}
T T=ofy().load().type(clazz).id(id).get();
如果(t!=null){
of y().delete().entity(t).now();
}
}
@凌驾
public void deleteByKey(类clazz,字符串键)引发DatabaseException{
if(key==null){
抛出新的DatabaseException(“ID不能为null”);
}
T T=ofy().load().type(clazz).id(key.get();
如果(t!=null){
of y().delete().entity(t).now();
}
}
}

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